基类封装BaseVC、NavVC、TabBarVC

一、GTBaseViewController

自定义左右返回键、添加MBProgressHUD、展示和隐藏导航栏。

自定义左右返回键

//自定义左右侧按钮
- (void)setupLeftAndRightBarButton {
    
    [self.rightButton addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    self.navigationItem.rightBarButtonItem = item;
    
    [self.leftButton addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];
    self.navigationItem.leftBarButtonItem = item2;
    
}

//左边按钮事件
- (void)leftItemClick:(UIButton *)click
{
    [self.navigationController popViewControllerAnimated:YES];  
}

 //按钮点击事件
    - (void)rightItemClick:(UIButton *)click{
    self.rightButton.hidden = YES;
}

//设置左边按键
-(UIButton*)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [_leftButton setImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
        [_leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 6)];
    }
    return _leftButton;
}

添加MBProgressHUD

#import "MBProgressHUD+BWMExtension.h"
#import "MBProgressHUD+FHLoading.h"

//
    - (MBProgressHUD*) showLoadingViewWithTitle:(NSString *)title
{
    [self hideHUD];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    NSString * bundlePath = [[NSBundle mainBundle] pathForResource: @"loging--" ofType :@"gif"];
    NSData  *imageData = [NSData dataWithContentsOfFile:bundlePath];
    hud.customView = nil;
    hud.color = [UIColor clearColor];
    hud.labelText = title;
    hud.labelColor = [UIColor blackColor];
    hud.labelFont = [UIFont systemFontOfSize:14];
    hud.opacity = 1;
    return hud;
}
//
    - (MBProgressHUD *) showHUDWithTitle:(NSString *)title type:(BWMMBProgressHUDMsgType)type {
    return [self showHUDWithTitle:title hideAfterDelay:kHudAutoHideDelay type:type completion:nil];
}
//
    - (MBProgressHUD *) showHUDWithTitle:(NSString *)title
                                type:(BWMMBProgressHUDMsgType)type
                          completion:(FHVoidBlock)completion {
    return [self showHUDWithTitle:title hideAfterDelay:kHudAutoHideDelay type:type completion:completion];
}
//
    - (MBProgressHUD *) showHUDWithTitle:(NSString *)title
                      hideAfterDelay:(NSTimeInterval)delay
                                type:(BWMMBProgressHUDMsgType)type
                          completion:(FHVoidBlock)completion {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    MBProgressHUD *hud = [MBProgressHUD bwm_showTitle:title toView:kKeyWindow hideAfter:delay msgType:type];
    if (delay > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            kSafeBlock(completion);
        });
    }
    return hud;
}
//
    - (void)hideHUD {
    [MBProgressHUD hideHUDForView:kKeyWindow animated:YES];
}
//
    - (void)hideAllHUD {
    [MBProgressHUD hideAllHUDsForView:kKeyWindow animated:YES];
}

展示和隐藏导航栏

///隐藏导航
    - (void)hiddenNavBar {
    [self.navigationController setNavigationBarHidden:YES animated:nil];
}

//导航透明
    - (void)navigationBarTransparent {
    ///设置导航栏变透明
    //设置导航栏背景图片为一个空的image,这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];    
}

//导航不透明
    - (void)navigationBarNo {
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

//导航白色
    - (void)navigationBarWhile {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
       [self.navigationController.navigationBar setShadowImage:nil];
}

//设置状态栏风格
    - (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

//隐藏返回按钮
    - (void)hiddenBackItem {
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem=nil;
}

//隐藏右侧按钮
    - (void)hiddenRigthItem {
    self.navigationItem.rightBarButtonItem=nil;
}

二、GTBaseNavViewController

添加左滑手势禁用、重写pushViewController方法隐藏底部tabbar、屏幕支持旋转方向、

重写pushViewController方法隐藏底部tabbar

//重写pushVC方法,pushVC后隐藏底部taBBar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
    
}

//重写PopVC方法
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    
    return  [super popViewControllerAnimated:animated];
    
}

添加左滑手势启动或禁用

- (void)configNvagation
{
    //启用左滑返回
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
    //是否隐约显示底层的视图
    self.extendedLayoutIncludesOpaqueBars = YES;
// 设置nva的title文字大小
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithHexString:@"#323232"], NSForegroundColorAttributeName,[UIFont fontWithName:@"PingFangSC" size: 18],NSFontAttributeName, nil]];
    }else{
      
    }
}
//
      - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        // 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
        return self.childViewControllers.count > 1;
    }
    return YES;
}
//
      - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //set gesture yes when showViewController

    // if rootViewController, set delegate nil
    if (navigationController.viewControllers.count == 1) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
        navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

屏幕支持旋转方向

//
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
    - (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

三、UITabBarController的封装

底部标签包含UITabBar标签条/UITabBarController标签页控制器/UITabBarItem标签项/UITabBarAppearance 三个文件。

自定义GTBaseTabBarViewController

添加子控制器

//tab1
    MainViewController *tab1VC = [[MainViewController alloc]init];
    [self setViewController:tab1VC title:@"" image:@"Home" selectImage:@"HomeN"];

#pragma mark - 添加子控制器
-(void)setViewController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
    static NSInteger index = 0;
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.tag = index;
    index++;
    GTBaseNavigationViewController *nav = [[GTBaseNavigationViewController alloc]initWithRootViewController:viewController];
    [self addChildViewController:nav];
}

你可能感兴趣的:(基类封装BaseVC、NavVC、TabBarVC)