123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- //
- // KSBaseViewController.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/17.
- //
- #import "KSBaseViewController.h"
- #import "Reachability.h"
- #import <KSToolLibrary/UIButton+EnlargeEdge.h>
- #import <KSToolLibrary/UIView+Hints.h>
- #import "LoginViewController.h"
- #import "CustomNavViewController.h"
- #import <KSToolLibrary/DiskFreeSpaceManager.h>
- #import "KSTabBarViewController.h"
- #import "KSImageAlert.h"
- #import <KSToolLibrary/UIImage+Color.h>
- #import "HomeViewController.h"
- #define PROMPT_TIME 1.5f
- @interface KSBaseViewController ()
- {
- MBProgressHUD *t_MBProgressHUD;
- }
- @property (nonatomic, strong) UIView *promptPlaceView;
- @end
- @implementation KSBaseViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.view.backgroundColor = HexRGB(0xf6f8f9);
- [self modifyNavigationBarColor:HexRGB(0xf6f8f9)];
- [[self.navigationController navigationBar] setTranslucent:NO];
- [self defaultMessage]; // 设置默认刷新数据
- _scrollView = [[UIScrollView alloc] init];
- [self.view addSubview:_scrollView];
- [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self.view);
- make.top.mas_equalTo(self.view.mas_top);
- make.bottom.mas_equalTo(self.view.mas_bottom);
- }];
- _scrollView.backgroundColor = [UIColor clearColor];
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.showsVerticalScrollIndicator = NO;
-
- self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
-
- if (self.navigationController && self != self.navigationController.viewControllers[0]) {
-
- [self leftbutton];
- }
- }
- - (void)modifyNavigationBarColor:(UIColor *)barColor {
- if (@available(iOS 13.0, *)) {
- UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
- [appearance configureWithOpaqueBackground];
- // 去掉半透明效果
-
- appearance.backgroundEffect = nil;
- // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线)
- appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
- appearance.backgroundColor = barColor;
- [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:18]}];
-
- self.navigationController.navigationBar.standardAppearance = appearance;
- self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
- }
- else {
- UINavigationBar *navBar = self.navigationController.navigationBar;
- [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:18]}];
- [navBar setBarTintColor:barColor];
- }
- }
- - (BOOL)networkAvaiable {
- return [self checkNetworkAvaiable];
- }
- - (void)defaultMessage {
- self.pages = 1;
- self.rows = 10;
- self.isLoadMore = YES;
- }
- - (void)selectBarHomeWithIndex:(NSInteger)index {
- self.navigationController.tabBarController.hidesBottomBarWhenPushed = NO;
- [(KSTabBarViewController *)self.tabBarController tabBarSelectedWithIndex:index];
- }
- - (void)freeSizeCheck {
- CGFloat freeSize = [DiskFreeSpaceManager QueryDiskSpaceFree];
- if (freeSize < 0.8) {
- [self showFreeSizeHardTips];
- }
- }
- - (void)showFreeSizeHardTips {
- KSImageAlert *alertView = [KSImageAlert shareInstance];
- MJWeakSelf;
- alertView.titleLabel.text = @"可用存储空间不足,可能会影响您的正常使用。您可以在设置-通用-储存空间中管理储存空间";
- [alertView.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
- [alertView.sureButton setTitle:@"设置" forState:UIControlStateNormal];
- [alertView sureCallback:^(BOOL isSure) {
- if (isSure) {
- [weakSelf openSettingView];
- }
- }];
- [alertView showAlert];
- }
- - (void)addfeedBackGenertor {
- if (@available(iOS 10.0, *)) {
- UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
- [feedBackGenertor impactOccurred];
- } else {
- // Fallback on earlier versions
- }
- }
- - (void)openSettingView {
- if (@available(iOS 10, *)) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- }
- - (void)allocTitle:(NSString *)titleStr {
- UILabel *navText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
- navText.backgroundColor = [UIColor clearColor];
- navText.textAlignment = NSTextAlignmentCenter;
- navText.font = [UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium];
- navText.textColor = HexRGB(0x000000);
- navText.text = titleStr;
- self.navigationItem.titleView = navText;
- }
- - (void)allocTitle:(NSString *)Ntitle withColor:(UIColor *)color {
- UILabel *natext= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
- natext.textAlignment = NSTextAlignmentCenter;
- natext.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
- natext.textColor = color;
- natext.text = Ntitle;
- self.navigationItem.titleView = natext;
- }
- - (void)leftbutton {
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_black"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
- leftButton.imageInsets = UIEdgeInsetsMake(0, -5, 0, 0);
- leftButton.tintColor = [UIColor blackColor];
- self.navigationItem.leftBarButtonItem = leftButton;
- }
- - (void)leftButtonWithImageName:(NSString *)imageName {
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
- leftButton.imageInsets = UIEdgeInsetsMake(0, -5, 0, 0);
- leftButton.tintColor = HexRGB(0x000000);
- self.navigationItem.leftBarButtonItem = leftButton;
- }
- // 判断当前是否Wi-Fi环境
- - (BOOL)isWiFiEnable {
- BOOL isWiFi = NO;
- Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
- switch ([reach currentReachabilityStatus]) {
- case NotReachable:
- isWiFi = NO;
- //NSLog(@"notReachable");
- break;
- case ReachableViaWiFi:
- isWiFi = YES;
- //NSLog(@"WIFI");
- break;
- case ReachableViaWWAN:
- isWiFi = NO;
- //NSLog(@"3G");
- break;
- }
- return isWiFi;
- }
- - (void)backAction {
- [self.view endEditing:YES];
- // 根据需要返回到不同页面
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void)rightButton:(UIImage*)image {
- UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
- rightBt.frame =CGRectMake(0, 0, 80, 40);
- rightBt.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -60);
- [rightBt setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
- rightBt.titleLabel.font = [UIFont systemFontOfSize:16];
- [rightBt setTitleColor:HexRGB(0x000000) forState:UIControlStateNormal];
- [rightBt setImage:image forState:UIControlStateNormal];
- [rightBt addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
- self.navigationItem.rightBarButtonItem = rightItem;
- }
- - (void)rightButtonTitle:(NSString *)title {
- [self rightButtonTitle:title color:HexRGB(0x000000)];
- }
- - (void)rightButtonTitle:(NSString *)title color:(UIColor *)titleColor {
- UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
- rightBt.frame =CGRectMake(0, 0, 80, 40);
- rightBt.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
- [rightBt setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
- rightBt.titleLabel.font = [UIFont systemFontOfSize:16];
- [rightBt setTitleColor:titleColor forState:UIControlStateNormal];
- [rightBt setTitle:title forState:UIControlStateNormal];
- [rightBt addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
- self.navigationItem.rightBarButtonItem = rightItem;
- }
- //结束页面编辑
- - (void)endViewEditing {
- [self.view endEditing:YES];
- }
- - (void)rightBtnClick{
-
- }
- -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
-
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = size.width;
- CGFloat targetHeight = size.height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
- if (CGSizeEqualToSize(imageSize, size) == NO)
- {
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
- if (widthFactor > heightFactor)
- scaleFactor = widthFactor; // scale to fit height
- else
- scaleFactor = heightFactor; // scale to fit width
- scaledWidth= width * scaleFactor;
- scaledHeight = height * scaleFactor;
- // center the image
- if (widthFactor > heightFactor)
- {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }
- else if (widthFactor < heightFactor)
- {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
- UIGraphicsBeginImageContext(size); // this will crop
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width= scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- if(newImage == nil)
- NSLog(@"could not scale image");
-
- //pop the context to get back to the default
- UIGraphicsEndImageContext();
-
- return newImage;
- }
- #pragma mark ---- 网络连接状态判断
- -(BOOL) isConnectionAvailable{
-
- BOOL isExistenceNetwork = YES;
- Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
- switch ([reach currentReachabilityStatus]) {
- case NotReachable:
- isExistenceNetwork = NO;
- //NSLog(@"notReachable");
- break;
- case ReachableViaWiFi:
- isExistenceNetwork = YES;
- //NSLog(@"WIFI");
- break;
- case ReachableViaWWAN:
- isExistenceNetwork = YES;
- //NSLog(@"3G");
- break;
- }
-
- if (!isExistenceNetwork) {
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- hud.removeFromSuperViewOnHide =YES;
- hud.mode = MBProgressHUDModeText;
- hud.label.text = NSLocalizedString(@"当前没有网络,请先连接网络", nil);
- hud.minSize = CGSizeMake(132.f, 108.0f);
- hud.label.textColor = [UIColor whiteColor];
- hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
- hud.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8);
- [hud hideAnimated:YES afterDelay:5];
- return NO;
- }
-
- return isExistenceNetwork;
- }
- - (BOOL)checkNetworkAvaiable {
- BOOL isExistenceNetwork = YES;
- Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
- switch ([reach currentReachabilityStatus]) {
- case NotReachable:
- isExistenceNetwork = NO;
- //NSLog(@"notReachable");
- break;
- case ReachableViaWiFi:
- isExistenceNetwork = YES;
- //NSLog(@"WIFI");
- break;
- case ReachableViaWWAN:
- isExistenceNetwork = YES;
- //NSLog(@"3G");
- break;
- }
- return isExistenceNetwork;
- }
- //打电话
- - (void)CallPhoneWith:(NSString*)phoneNumber {
- phoneNumber = [phoneNumber replaceAll:@" " WithString:@""];
- NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
- //方法一
- [[UIApplication sharedApplication] openURL:phoneURL]; //拨号
- }
- #pragma mark --- 无数据 显示
- /**
- 设置没有数据时的显示
-
- @param promptString 提示语
- @param imgName 图片名称
- @param view 显示在什么地方
- */
- - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
- if (self.promptView != nil) {
- [self.promptView removeFromSuperview];
- }
- else {
- self.promptView = [[StateView alloc]init];
- }
- _promptPlaceView = view;
- //当请求不到数据时 ,自定义提示view 将会出现;
- self.promptView.imageName = imgName;
- self.promptView.alpha = 0.0f;
- [self.promptView setText:promptString];
- [view addSubview:self.promptView];
- [self.promptView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_equalTo(view);
- make.width.mas_equalTo(view);
- make.height.mas_equalTo(view);
- }];
- }
- // 结束刷新后调用方法
- - (void)changePromptLabelState {
- NSInteger count;
- if (self.dataArray.count) {
- count = self.dataArray.count;
- } else {
- count = 0;
- }
-
- [UIView animateWithDuration:0.1 animations:^{
- [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
- }] ;
-
- }
- - (void)resetParamenter {
- self.pages = 1;
- self.isLoadMore = YES;
- self.dataArray = [NSMutableArray array];
- }
- #pragma 检测 和跳转登录界面
- - (BOOL)checkIsLoginToLoginView:(BOOL)toLogin {
- NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:TokenKey];
- if (token.length == 0 || token == nil) { // 未登录
- _isLogin = NO;
- if (toLogin) { // 需要去登录页面
-
- }
- }
- else { // 登录状态
- _isLogin = YES;
- }
- return _isLogin;
- }
- // 首页去登录页面
- - (void)showLoginView {
- LoginViewController *logonVC = [[LoginViewController alloc] init];
- CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:logonVC];
- // logonVC.homeLogin = YES;
- navCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:navCtrl animated:YES completion:nil];
- }
- #pragma mark -- lazying
- - (NSMutableArray *)dataArray{
- if (!_dataArray) {
- _dataArray =[NSMutableArray array];
- }
- return _dataArray;
- }
- // 移除通知
- - (void)removeNotifier {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- /// 获取上一个视图控制器
- - (KSBaseViewController *)getPreViewController {
- NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self];
- if (currentIndex > 0 && currentIndex < self.navigationController.viewControllers.count && currentIndex != NSNotFound) {
- // 获取上一个视图控制器
- return [self.navigationController.viewControllers objectAtIndex:currentIndex - 1];
- }
- return nil;
- }
- /// 获取下一个视图控制器
- - (KSBaseViewController *)getNextViewController {
- NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其
- if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { //当前视图控制器在栈中,故为push操作
- return [self.navigationController.viewControllers objectAtIndex:viewControllers.count - 1];
- }
- else {
- return [self getPreViewController];
- }
- }
- // viewWillDisappear 使用
- - (BOOL)isViewPopDismiss {
- NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其
- if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { //当前视图控制器在栈中,故为push操作
- return NO;
- } else if ([viewControllers indexOfObject:self] == NSNotFound) { //当前视图控制器不在栈中,故为pop操作
- return YES;
- }
- return NO;
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- self.preCtrl = [self getPreViewController];
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|