美文网首页
设置状态栏字体颜色

设置状态栏字体颜色

作者: 喝酸奶舔下盖 | 来源:发表于2020-09-03 15:41 被阅读0次

状态栏的字体为黑色:UIStatusBarStyleDefault(iOS13 用UIStatusBarStyleDarkContent)
状态栏的字体为白色:UIStatusBarStyleLightContent
View controller-based status bar appearance项设为YES,则View controller对status bar的设置优先级高于application的设置。
为NO则以application的设置为准,view controller的prefersStatusBarHidden方法无效,是根本不会被调用的

一、在info.plist中,将View controller-based status bar appearance设为NO

1.在info.plist中,将View controller-based status bar appearance设为NO.
2.在app delegate中设置APP默认通用的字体颜色:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; // 字体颜色白色
3.在个别状态栏字体颜色不一样的VC中
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self defaultStatusColor];  // 字体颜色黑色
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self lightStatusColor];   // 白色颜色白色
}

UIViewController+Tools.h 文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (Tools)

/**
 * 默认顶部状态条颜色
 */
-(void)defaultStatusColor;
/**
 * 高亮顶部状态条颜色
 */
-(void)lightStatusColor;

@end

NS_ASSUME_NONNULL_END

UIViewController+Tools.m

#import "UIViewController+Tools.h"

@implementation UIViewController (Tools)

#pragma mark - 状态条颜色
/**
 * 默认顶部状态条颜色
 */
-(void)defaultStatusColor{
    if (@available(iOS 13.0, *)) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;//文字是黑色
    } else {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;//文字黑色
    }
}
/**
 * 高亮顶部状态条颜色
 */
-(void)lightStatusColor{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}


@end

statusBarStyle在iOS9之后,苹果已经不推荐使用这些方法了,建议使用下面方法二

// Setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic) UIStatusBarStyle statusBarStyle API_DEPRECATED("Use -[UIViewController preferredStatusBarStyle]", ios(2.0, 9.0)) API_UNAVAILABLE(tvos);
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated API_DEPRECATED("Use -[UIViewController preferredStatusBarStyle]", ios(2.0, 9.0)) API_UNAVAILABLE(tvos);

二、在info.plist中,将View controller-based status bar appearance设为YES,或者没有设置。

View controller-based status bar appearance的默认值就是YES。
如果View controller-based status bar appearance为YES。
则[UIApplication sharedApplication].statusBarStyle 无效。
可以使用下面的方法:

1.在vc中重写vc的preferredStatusBarStyle方法。
-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
2.在viewDidload中调用:[self setNeedsStatusBarAppearanceUpdate];

但是,当vc在nav中时,上面方法没用,vc中的preferredStatusBarStyle方法根本不用被调用。
原因是,[self setNeedsStatusBarAppearanceUpdate]发出后,只会调用navigation controller中的preferredStatusBarStyle方法,vc中的preferredStatusBarStyley方法跟本不会被调用。
解决办法有两个:

方法一:

设置navbar的barStyle 属性会影响status bar 的字体和背景色。如下。

//status bar的字体为白色

//导航栏的背景色是黑色。
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//status bar的字体为黑色
//导航栏的背景色是白色,状态栏的背景色也是白色。
//self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
方法二:

自定义一个nav bar的子类,在这个子类中重写preferredStatusBarStyle方法:

CustomNav *nav = [[CustomNav alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;

@implementation CustomNav

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController* topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}

相关文章

网友评论

      本文标题:设置状态栏字体颜色

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