ios ~ 设置BaseViewcontroller:两种方法(系统、自定义)

  • 首页跳转下一页的时候,push前隐藏底下的tabBar
GWTeamInfoViewController *teamInfoVC = [[GWTeamInfoViewController alloc]initWithTeamId:teamId];
    [teamInfoVC setHidesBottomBarWhenPushed:YES]; // 跳到下一页,隐藏底下的tabBar
    [self.navigationController pushViewController:teamInfoVC animated:YES];
注意:侧滑返回上一页(在基类的viewDidAppear中设置YES可侧滑,在禁止侧滑的VC中的viewDidAppear方法中设置NO),如果tabbar那几个首页VC也是使用的基类VC设置侧滑的话,将他们单独设置interactivePopGestureRecognizer = NO,否则在其页面,侧滑之后,点击不了控件。
#pragma mark 视图已经加入到窗口时

-(void)viewDidAppear:(BOOL)animated{

    NSLog(@"视图已经加入到窗口时");

    // 是否允许右滑返回:YES 启动侧滑,NO 禁止侧滑
    [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];

}

  • 第一种:使用系统的导航栏

正式代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    // 设置导航栏下边线(投影)去掉
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    // 设置导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setTintColor:RGBA(0, 0, 0, 1)];
    // 设置导航栏默认标题的颜色和字体的大小
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:RGBA(0, 0, 0, 1), NSFontAttributeName:[UIFont systemFontOfSize:([UIScreen mainScreen].bounds.size.width / 375)*18 weight:UIFontWeightMedium]};

    /// iOS 13.0 之后的新特性:
    NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:RGBA(0, 0, 0, 1), NSFontAttributeName:[UIFont systemFontOfSize:([UIScreen mainScreen].bounds.size.width / 375)*18 weight:UIFontWeightMedium]};
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        appearance.backgroundColor = [UIColor whiteColor]; // 导航栏背景颜色
        appearance.titleTextAttributes = titleTextAttributes; // 设置导航栏默认标题的颜色和字体的大小
        appearance.shadowColor = [UIColor clearColor]; // 设置导航栏底部的分割线不显示
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        
        
    } else {
        // Fallback on earlier versions
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        [self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
    }
    
    self.navigationController.navigationBarHidden = NO;
    
    // 去掉导航栏上【返回】按钮 后面的文字
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButtonItem];
//    self.navigationController.navigationBar.topItem.title = @"";
    
    UIButton *leftBut = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 16)];
    [leftBut setImage:[UIImage imageNamed:@"back_black_icon"] forState:UIControlStateNormal];
    [leftBut addTarget:self action:@selector(clickBackLastPageAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:leftBut];
    self.navigationItem.leftBarButtonItem = backBar;
    
    // self.navigationItem.leftItemsSupplementBackButton = NO; // 设置YES,以达到页面可以左滑的目的(但是会有系统"<")
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; // 自定义 按钮,并实现右滑返回效果(注意:如果不写“(id)”的话,就需要遵守代理UIGestureRecognizerDelegate)
    self.edgesForExtendedLayout = UIRectEdgeNone; // self.view 的坐标在(0,导航栏高度+状态栏高度)
//    self.navigationController.navigationBar.translucent = NO;
/**
    // 禁止“右滑侧边”返回上一页
    if (self.navigationController.interactivePopGestureRecognizer) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    */

    /*
    // 替换系统的“<"图标
    [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"back_black_icon"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_black_icon"]];
    self.navigationItem.backBarButtonItem = backBar;
    */
    /**
    // 在有UIScrollview,并且是左右滑动的时候:以免侧滑失效,进行一下操作:
    // 手势互斥(侧滑返回手势失效后才响应UITableView的滑动手势)
    [self.myScrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
*/
    
}

- (void)clickBackLastPageAction {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)toRootViewController {
    UIViewController *viewController = nil;
    while (viewController.presentationController) {
        // 判断是否为最底层控制器
        if ([viewController isKindOfClass:[LRBaseViewController class]]) {
            viewController = viewController.presentingViewController;
            // self.navigationItem.title = @"";
        } else {
            break;
        }
    }
    if (viewController) {
        [viewController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.nowViewCTRL = self;
}
隐藏导航栏下边线:某一个页面中设置导航栏下边线的时候
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    // 在该页面显示:
    self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@""];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
     // 退出该页面隐藏:
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
  • 第二种:使用自定义的导航栏

(原理:将系统的导航栏隐藏,自定义一个UIView和其上边的按钮,即LeftButton和RightButton等等需要的按钮,和标题)

下面代码部分:

BaseViewController.h
#import 

NS_ASSUME_NONNULL_BEGIN

@interface BaseViewController : UIViewController
/**
 隐藏导航栏 位置占着,隐藏了view
 */
@property (assign,nonatomic) BOOL hiddenNavigationBar;

/** 添加一个半透明的遮盖层:状态栏和导航栏位置,随着滑动,可以改变其透明度 */
@property (nonatomic, strong) UIView * navigationBack_TranslucentView;

@property (strong,nonatomic) UIView * statusBg;
@property (strong,nonatomic) UIView * navigationBar;

@property (strong,nonatomic) UIView * contentBg;

/**
 带圆角的view,全局用
 */
@property (strong,nonatomic) UIView * circularBackgroundView;

/**
 标题Label
 */
@property (strong,nonatomic) UILabel * titleLabel;

/**
 左侧图片 用于左侧图片+title时的表现
 */
@property (strong,nonatomic) UIImageView * leftImage;

/**
 左侧按钮,在animationType是0时默认是返回按钮
 */
@property (strong,nonatomic) UIButton * leftButton;

/**
 最右面按钮,在animationType是1时默认是返回按钮
 */
@property (strong,nonatomic) UIButton * rightButton;

/**
 下方横线
 */
@property (strong,nonatomic) UIView * naviBarBottomLineView;

/**
导航栏下面如果是圆角,就把这个属性设置成YES
*/
@property (assign,nonatomic) BOOL isRoundTop;

- (void)back;


@end

NS_ASSUME_NONNULL_END

BaseViewController.m
#import "BaseViewController.h"
#import "UIView+Extension.h"

@interface BaseViewController () 
@property (strong,nonatomic) UIView * redBg;
@property (strong,nonatomic) UIView * rightButtonsBg;
@property (strong,nonatomic) UIView * rightButtonsBgLine;

@end 

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorBaseNaviBarBackground;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; // 右滑返回上一页
    
    /** 添加一个半透明的遮盖层:状态栏和导航栏位置,随着滑动,可以改变其透明度 */
    _navigationBack_TranslucentView = [[UIView alloc] init];
    _navigationBack_TranslucentView.backgroundColor = RGBA(248, 246, 242, 0); // 设置全透明
    self.navigationBack_TranslucentView.hidden = YES; // 默认隐藏,有需要时显示,并改变其透明度
    [self.view addSubview:self.navigationBack_TranslucentView];
    [self.navigationBack_TranslucentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(k_Height_StatusBar+k_Height_NavContentBar);
    }];

    /**
     // 使用方法:
     self.navigationBack_TranslucentView.hidden = NO;
     self.navigationBack_TranslucentView.backgroundColor = RGBA(248, 246, 242, 0);
     
     // 当UITableView滑动时,逐渐改变其透明度
     - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
         if (scrollView == self.tableView) {
             if (self.tableView.contentOffset.y <= 0) {
                 self.navigationBack_TranslucentView.backgroundColor = RGBA(248, 246, 242, 0);
             } else {
                 self.navigationBack_TranslucentView.backgroundColor = RGBA(248, 246, 242, self.tableView.contentOffset.y * 0.01);
             }
         }
     }
     */

    //navigationbar上面部分
    _statusBg = [[UIView alloc]init];
    _statusBg.backgroundColor = ColorBaseNaviBarBackground;
    [self.view addSubview:_statusBg];
    [_statusBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(k_Height_StatusBar));
    }];
    
    //上面背景
    _navigationBar = [[UIView alloc] init];
    _navigationBar.userInteractionEnabled = YES;
    self.navigationBar.backgroundColor = ColorBaseNaviBarBackground;
    [self.view addSubview:self.navigationBar];
    
    [_navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.statusBg.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(k_Height_NavContentBar));
    }];
    
    
    _circularBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, k_Height_NavBar, SCREEN_WIDTH, SCREEN_HEIGHT-k_Height_NavBar)];
    _circularBackgroundView.backgroundColor = [UIColor whiteColor];
    _circularBackgroundView.hidden = YES;
    UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:_circularBackgroundView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(30, 30)];

    CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
    cornerRadiusLayer.frame = _circularBackgroundView.bounds;
    cornerRadiusLayer.path = cornerRadiusPath.CGPath;
    _circularBackgroundView.layer.mask = cornerRadiusLayer;
    [self.view addSubview:self.circularBackgroundView];
    

    
    //左侧图标
    _leftImage = [[UIImageView alloc]init];
    //_leftImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.navigationBar addSubview:_leftImage];
    
    //push时隐藏
    _leftImage.hidden = YES;
    
    [_leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.navigationBar).offset(6);
        make.centerY.equalTo(self.navigationBar);
        make.width.height.equalTo(@32);
        //        make.left.top.bottom.equalTo(self.navigationBar);
        //        make.width.equalTo(self.leftImage.mas_height);
    }];
    
    //左侧返回
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_leftButton setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [self.navigationBar addSubview:_leftButton];
    
    //非push时隐藏
    _leftButton.hidden = NO;
    _leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_leftButton addTarget:self action:@selector(clickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.navigationBar);
        make.centerY.equalTo(self.navigationBar);
        make.width.offset(APP_transverse_Scale(41));
        make.height.equalTo(self.navigationBar);
    }];
    _naviBarBottomLineView = [[UIView alloc]init];
    _naviBarBottomLineView.backgroundColor = RGB(230, 230, 230);
    _naviBarBottomLineView.hidden =YES;
    [self.navigationBar addSubview:_naviBarBottomLineView];
    [_naviBarBottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.navigationBar);
        make.centerX.equalTo(self.navigationBar);
        make.bottom.equalTo(self.navigationBar);
        make.height.offset(APP_transverse_Scale(1));
    }];
    //左侧title
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.textColor = ColorBaseNaviBarTitle;
    _titleLabel.font = FontNaviBarTitle;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = @"";
    [self.navigationBar addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.navigationBar);
        make.centerY.equalTo(self.navigationBar);
        make.left.mas_equalTo(self.navigationBar.mas_left).offset(APP_transverse_Scale(44));
        make.right.mas_equalTo(self.navigationBar.mas_right).offset(APP_transverse_Scale(-44));
    }];
    
    //右上按钮背景
    _rightButtonsBg = [[UIView alloc]init];
    _rightButtonsBg.backgroundColor = RGBA(0, 0, 0, .5);
    _rightButtonsBg.layer.cornerRadius = 16;
    _rightButtonsBg.layer.masksToBounds = YES;
    [self.navigationBar addSubview:_rightButtonsBg];
    
    _rightButtonsBg.backgroundColor = [UIColor clearColor];
    
    [_rightButtonsBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.navigationBar).offset(-APP_transverse_Scale(10));
        make.top.equalTo(self.navigationBar).offset(APP_transverse_Scale(6));
        make.bottom.equalTo(self.navigationBar).offset(-APP_transverse_Scale(6));
        make.width.offset(APP_transverse_Scale(78));
    }];
    
    //背景上的线
    _rightButtonsBgLine = [[UIView alloc]init];
    _rightButtonsBgLine.backgroundColor = [UIColor whiteColor];
    [self.rightButtonsBg addSubview:_rightButtonsBgLine];
    
    _rightButtonsBgLine.hidden = YES;
    
    [self.rightButtonsBgLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.rightButtonsBg).offset(APP_transverse_Scale(4));
        make.bottom.equalTo(self.rightButtonsBg).offset(-APP_transverse_Scale(4));
        make.width.equalTo(@1);
        make.centerX.equalTo(self.rightButtonsBg);
    }]; 
    
    //最右侧按钮
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightButton setImage:[UIImage imageNamed:@"icon_nav_close"] forState:UIControlStateNormal];
    [_rightButtonsBg addSubview:_rightButton];
    [_rightButton addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _rightButton.hidden = NO;
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.rightButtonsBg);
        make.top.bottom.equalTo(self.rightButtonsBg);
        make.width.equalTo(self.rightButton.mas_height);
    }];
    
    
    self.contentBg = [[UIView alloc]init];
    self.contentBg.layer.cornerRadius = 10;
    self.contentBg.layer.masksToBounds = YES;
    self.contentBg.backgroundColor = ColorBaseNaviBarBackground;
    [self.view addSubview:self.contentBg];
    
    [self.contentBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.navigationBar.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide).offset(APP_transverse_Scale(20));
    }];
    
    self.isRoundTop = self.isRoundTop;
    self.statusBg.backgroundColor = RGBA(221, 203, 177, 1);
    self.navigationBar.backgroundColor = RGBA(221, 203, 177, 1);
    self.titleLabel.textColor = RGBA(0, 0, 0, 1);
    
}
#pragma mark - setter/getter

- (void)setIsRoundTop:(BOOL)isRoundTop{
    
    _isRoundTop = isRoundTop;
    self.redBg.hidden = self.contentBg.hidden = isRoundTop == NO;
}
- (void)setHiddenNavigationBar:(BOOL)hiddenNavigationBar{
    
    _hiddenNavigationBar = hiddenNavigationBar;
    self.statusBg.hidden = self.navigationBar.hidden = hiddenNavigationBar;
}
#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:animated];
} 
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    // NSLog(@"视图已经加入到窗口时");
    
    // 是否允许右滑返回:YES 启动侧滑,NO 禁止侧滑
    [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];

}
- (void)clickLeftButton{
    [self back];
}

- (void)back;{
    [self.navigationController popViewControllerAnimated:YES]; 
}

- (void)rightBtnClick {
    
}



/** 横屏设置: */
#pragma mark //屏幕旋转
/// 决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
//- (BOOL)shouldAutorotate {
//    return NO; // NO 下面的方法不执行,YES 执行下面的方法 (supportedInterfaceOrientations)
//}
//
//// 2.返回支持的旋转方向
//// 设置当前界面支持的所有方向,所以返回值是UIInterfaceOrientationMask,更加方便的表达支持多方向旋转的情况。
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskPortrait; // 只支持这一个方向(正常的方向)
//}
//
//// 3.返回进入界面默认显示方向
//-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationPortrait;
//}



/*
#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

你可能感兴趣的:(ios ~ 设置BaseViewcontroller:两种方法(系统、自定义))