iOS状态栏配置

一、全局配置状态栏

  • info.plist中添加key:View controller-based status bar appearance,并设置value为NO

  • 在需要设置样式的地方调用:

    1
    2
    3
    //UIStatusBarStyleLightContent:Light content, for use on dark backgrounds
    //UIStatusBarStyleDefault:Dark content, for use on light backgrounds
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

二、配置单个控制器状态栏

  • info.plist中添加key:View controller-based status bar appearance,并设置value为YES

  • 如果有控制器没有导航控制器,直接重写preferredStatusBarStyle 方法返回你想要的状态栏样式即可:

    1
    2
    3
    - (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
    }
  • 通常都有导航控制器,如果控制器中直接重写preferredStatusBarStyle是没有效果的,这时需要在基类导航控制器中重写childViewControllerForStatusBarStylepreferredStatusBarStyle 任意一个方法就能实现配置单个控制器的状态栏样式:

    1
    2
    3
    4
    5
    6
    7
    8
    - (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
    }

    或者重写
    - (UIStatusBarStyle)preferredStatusBarStyle {
    return [self.topViewController preferredStatusBarStyle];
    }

    注意:两个方法都重写,只会调用childViewControllerForStatusBarStyle,所以需要自己根据清空去选择调用那个方法。

三、通过分类实现

  • 在项目中方便使用,我写了一个UINavigationController(WBStatusBarStyle) 分类,主要代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    @implementation UINavigationController (WBStatusBarStyle)

    + (void)wb_setDefaultStatusBarStyle:(UIStatusBarStyle)statusBarStyle {
    objc_setAssociatedObject(self, &kWBDefaultStatusBarStyleKey, @(statusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    + (UIStatusBarStyle)wb_DefaultStatusBarStyle {
    id style = objc_getAssociatedObject(self, &kWBDefaultStatusBarStyleKey);
    return style ? [style integerValue] : UIStatusBarStyleDefault;
    }

    /** < Override to return a child view controller or nil. If non-nil, that view controller's status bar appearance attributes will be used. If nil, self is used. Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes should be called. > */
    //- (UIViewController *)childViewControllerForStatusBarStyle {
    // return self.topViewController;
    //}
    //
    //- (UIViewController *)childViewControllerForStatusBarHidden {
    // return self.topViewController;
    //}

    - (UIStatusBarStyle)preferredStatusBarStyle {
    return [self.topViewController wb_statusBarStyle];
    }

    @end
  • UIViewController (WBStatusBarStyle)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@implementation UIViewController (WBStatusBarStyle)

- (void)setWb_statusBarStyle:(UIStatusBarStyle)wb_statusBarStyle {
objc_setAssociatedObject(self, &kWBStatusBarStyleKey, @(wb_statusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
/** < Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes should be called. > */
[self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)wb_statusBarStyle {
id style = objc_getAssociatedObject(self, &kWBStatusBarStyleKey);
return style ? [style integerValue] : [UINavigationController wb_DefaultStatusBarStyle];
}

@end

四、GitHub Demo

-------------本文结束感谢您的阅读-------------
分享到:
0%