iOS开发中全局修改UINavigationBar的样式

iOS开发中全局修改UINavigationBar的样式

本文主要介绍在开发中, 我们遇到需要修改UINavigationBar样式的时候, 一种全局的修改方式, 而且非常高效, 适用, 希望对大家有帮助.

前言

在iOS开发中, 我们会经常跟UINavigationController打交道, 那么修改UINavigationBar样式的事情也是经常遇到的, 我之前的做法就是: 在每个childViewController中, 逐个修改样式, 添加leftBarButtonItem 和 rightBarButtonItem等, 就像下面代码这样:

    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

这样的做法是非常低效的, 而且每个childController我们都需要设置, 如果我们需要自定义返回按钮, 这样的设置方式也是行不通的, 这种方式也会产生很多意想不到的问题, 相信使用过的朋友都有类似的经历.

接下来, 介绍一种合理的设置方式:

全局设置

这种方法就是通过[UINavigationBar appearance]和[UIBarButtonItem appearance]来进行全局的设置, 下面贴出一段我设置的代码:

    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    [UINavigationBar appearance].titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

    [UINavigationBar appearance].backIndicatorImage =
    [UINavigationBar appearance].backIndicatorTransitionMaskImage =
    [UIImage templateImageNamed:@"nav_bar_back_icon_white"];

    [[UINavigationBar appearance] setBarTintColor:MMA_BLACK(1)];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    // hide title of back button
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];

    NSShadow *clearShadow = [[NSShadow alloc] init];
    clearShadow.shadowColor = [UIColor clearColor];
    clearShadow.shadowOffset = CGSizeMake(0, 0);

    UIColor *normalTitleColor = [UIColor whiteColor];
    UIColor *highlightedTitleColor = [UIColor whiteColor];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{
                                                           NSForegroundColorAttributeName : normalTitleColor,
                                                           NSShadowAttributeName : clearShadow
                                                           } forState:UIControlStateNormal];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{
                                                           NSForegroundColorAttributeName : highlightedTitleColor,
                                                           NSShadowAttributeName : clearShadow
                                                           } forState:UIControlStateHighlighted];

    [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];

    [[UIToolbar appearance] setBarTintColor:[UIColor whiteColor]];

这个就需要我们在程序启动的时候设置, 也就是在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions 中设置, 这种设置方法的好处就是:

1.只需要设置一次, 不需要再每个childController中逐个设置

2.可以保证UINavigationBar的风格一致

3.通过这种方式可以自定义UINavigationController的返回键

总的来说, 建议大家通过这种方式设置, 这样其实能够很大程度上提高开发效率.

总结

建议大家讲上述设置代码, 写进UINavigationBar的Category中, 这样我们就可以实现复用, 这也是一种很好的编程习惯. 希望本文对在UINavigationBar样式设置上遇到问题的同学有帮助.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章