改变 NavigationBar 返回按钮的图标

这个问题是这篇所谓问题中我花费时间最长才解决的,对于初学者的我真是一个不小的坑。先说一下走过的弯路,一开始是通过继承UIViewController(比如叫MyViewController),并设置其navigationItem的属性leftBarButtonItem来实现的,而且必须要给这个按钮实现点击就返回的方法。这样做的缺点有:

  1. 所有放在 UINavigationController 中的 ViewController 都要继承了这个 MyViewController
  2. 如果是在 UINavigationController 的栈底的 ViewController,需要增加一个隐藏返回按钮的判断
  3. 如果从 MyViewController 及其子类 push 了一个 iOS 的原生界面,比如 UIImagePickerController,那么这个返回按钮就失效了

但是,终于让我找到了一个更简单并且解决以上所有缺点的方法,在 AppDelegate 中进行全局设置,代码如下:

UIImage *backImage = [[[UIImage imageNamed:@"navigation_back"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
    imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, 11.5, 0)];
[[UINavigationBar appearance] setBackIndicatorImage:backImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];

注:返回按钮的图片我采用的是44*44 point 的图片,但是不知道为什么如果直接设置就会偏上11.5 point,只好校正一下。

另外,如果想把返回按钮的文字隐藏,我只找到了这么一个 workaround 的奇技淫巧:

 [[UIBarButtonItem appearance] 
    setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
    forBarMetrics:UIBarMetricsDefault];

其实就是把按钮文字向上移了60 point,并没有隐藏,只是在屏幕上看不到而已,用 Reveal 还是可以看到……

改变 NavigationBar 的背景颜色

[UINavigationBar appearance].barTintColor = [UIColor blueColor];

改变 NavigationBar 的字体颜色

NavigationBar 上面有两处可以改变字体颜色,一是标题,二是左右按钮的文字。

改变按钮的文字颜色:

[UINavigationBar appearance].tintColor = [UIColor whiteColor];

改变标题的文字颜色:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

去掉 NavigationBar 下方的阴影

iOS 7 NavigationBar的下方默认是有一条阴影的,如果想要 NavigationBar 和下面内容的背景颜色融为一体的话,就要去掉这个阴影:

[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];

改变 TabBar 的字体颜色

[UITabBarItem.appearance setTitleTextAttributes:
     @{ NSForegroundColorAttributeName : [UIColor blueColor] }
                                       forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:
     @{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                       forState:UIControlStateSelected];

改变 StatusBar 的颜色

iOS7以后,status bar 的背景颜色变成了透明色,而且系统会根据 app的颜色自动改变 status bar 的字体颜色(黑和白)。但是这个自动改变的字体颜色并不一定和所有的 app 都搭配,比如我们 app 的主题色是稍微浅一丢丢的蓝,但是系统匹配的 status bar 的字体颜色就是黑色,看起来就很不爽,所以就要强制将其改为白色。

  1. 首先在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance的 item,其 Type 设为 Boolean,Value 设为 NO
  2. 然后在AppDelegate.mapplication:didFinishLaunchingWithOptions:中添加:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

隐藏 StatusBar

有时候为了实现沉浸式设计,比如 app 首次打开的引导页,需要隐藏整个 StatusBar,方法如下:

  1. 和改变 StatusBar 颜色一样,在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance的 item,其 Type 设为 Boolean,Value 设为 NO
  2. 在需要隐藏StatusBar 的 ViewController 中的viewDidLoad加入以下代码:
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
         [self prefersStatusBarHidden];
         [self setNeedsStatusBarAppearanceUpdate];
     }
    
  3. 重写prefersStatusBarHidden
    -(BOOL)prefersStatusBarHidden {
     return YES;
    }
    

注:但是这样设置的话从这个页面开始整个 app 的 StatusBar 都会隐藏,如果想要再显示出来,只需要在其他 ViewController 中加入:

[UIApplication sharedApplication].statusBarHidden = NO;

本文为博主原创,转载请保留链接,谢谢

你可能感兴趣的内容
0条评论

dexcoder

这家伙太懒了 <( ̄ ﹌  ̄)>
Owner