美文网首页
对navBar、tabBar的设置

对navBar、tabBar的设置

作者: 大成小栈 | 来源:发表于2022-01-25 20:28 被阅读0次

初始化时,对navBar、tabBar的设置

- (void)initRootViewController{
    
    // 初始化StatusBar,NavigationBar,TabBar
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self initNavgationBarStyle];
    [self handleTabbarStyle];
    
    // 初始化tabbarController
    self.rootWindow.backgroundColor = [UIColor whiteColor];
    DCTabBarController *targetTabbarController;

    self.tabbarController = [[DCTabBarController alloc] init];
    self.tabbarController.delegate = self;
    targetTabbarController = self.tabbarController;

    self.rootWindow.rootViewController = targetTabbarController;
    [self.rootWindow makeKeyAndVisible];
    [UIApplication sharedApplication].delegate.window = self.rootWindow;
}

-(void)initNavgationBarStyle{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[UINavigationBar appearance] setBarTintColor:UIColor.whiteColor/*RGBCOLOR(250,248,245)*/];
    [[UINavigationBar appearance] setBackgroundColor:UIColor.whiteColor];
    [[UINavigationBar appearance] setTintColor:UIColor.grayColor];
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
    //导航背景色
    [[UINavigationBar appearance] setBackgroundImage:[UIImage createImageWithColor:UIColor.whiteColor]
                                       forBarMetrics:UIBarMetricsDefault];
    //    [UINavigationBar appearance].shadowImage = [[UIImage alloc] init];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 0);
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor dj_colorWithHex:@"#333333"]/*RGBCOLOR(138,58,82)*/, NSForegroundColorAttributeName,
                                                           shadow, NSShadowAttributeName,
                                                           [UIFont systemFontOfSize:17.0f],
                                                           NSFontAttributeName, nil]];
    //bar在屏幕顶部且它的背景向上延伸进入状态栏区域
    [[UINavigationBar appearance] backgroundImageForBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
}

#pragma mark - Tabbar样式
- (void)handleTabbarStyle {
    [[UITabBar appearance] setBackgroundColor:[UIColor dj_colorWithHex:@"#FFFFFF"]];
    if (self.simplizeEnable) {
        UIColor *titleHighlightedColor = [UIColor dj_colorWithHex:@"#333333"];
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           titleHighlightedColor, NSForegroundColorAttributeName,
                                                           DJUIStd.sharedstd.FontH3.Medium, NSFontAttributeName,
                                                           nil] forState:UIControlStateNormal];
        
        UIColor *selectColor = [UIColor dj_colorWithHex:@"#028926"];
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           selectColor, NSForegroundColorAttributeName,
                                                           DJUIStd.sharedstd.FontH3.Medium, NSFontAttributeName,
                                                           nil] forState:UIControlStateSelected];
    }else{
        UIColor *titleHighlightedColor = [UIColor dj_colorWithHex:@"#666666"]/*RGBCOLOR(152, 152, 152)*/;
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           titleHighlightedColor, NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateNormal];
        
        UIColor *selectColor = [UIColor dj_colorWithHex:@"#00CF37"]/*RGBCOLOR(138,58,82)*/;
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           selectColor, NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateSelected];
    }
}

还可以在delegate中设置tabBar的样式,请参考:
https://www.jianshu.com/p/b3f1ff9d84bd

1、透明导航栏+定制title颜色

// 重写需要使用透明导航栏的VC的viewWillAppear方法
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
}

2、首页透明导航栏,push进一个默认导航栏的页面

// 重写需要使用默认导航栏的VC的viewWillAppear方法
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = nil;// 要使用默认导航栏页面的话,需要设置为nil,否则没有导航栏下面的那根线
    self.navigationController.navigationBar.translucent = NO;
}
// 既然在首页的viewWillAppear写了透明的代码,为什么还要在这里写呢?我在尝试的时候发现,不这样的话,透明的效果会晚一点生效,看起来不自然
- (void)willMoveToParentViewController:(UIViewController *)parent{
    if (!parent){
        self.navigationController.navigationBar.translucent = YES;
        self.navigationController.navigationBar.shadowImage = [UIImage new];
    }
}

不重写willMoveToParentViewController的样子:点击test,然后点返回按钮,导航栏的透明效果生效的不及时,如果使用滑动返回手势,透明效果生效及时


改后的效果

3、一个坑:
A页导航栏文字白色,push进B页面,颜色使用黑色,然后点击返回按钮返回,导航栏文字设为白色无效!!!首页的viewWillAppear执行了,但是不起作用。(使用滑动返回手势,是起作用的)

// A页
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
}
// B页
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor]}; // title颜色
}

解决方法同2,要在B页里重写方法(同2中改后的效果):

- (void)willMoveToParentViewController:(UIViewController *)parent{
    if (!parent){
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
    }
}

4、A页Push进B页,去掉返回按钮中的文字,仅保留箭头,并且定制箭头颜色

// A页
- (void)gotoB{
    
    [self performSegueWithIdentifier:@"gotoB" sender:nil];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]init];
    backButton.title = @"";
    self.navigationItem.backBarButtonItem = backButton;
}
// B页
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:228.0/255 green:156.0/255 blue:0 alpha:1];
}

参考文章:https://www.jishudog.com/6837/html

相关文章

网友评论

      本文标题:对navBar、tabBar的设置

      本文链接:https://www.haomeiwen.com/subject/udfghrtx.html