繁体   English   中英

更改状态栏颜色 Objective-C

[英]Change status bar color Objective-C

在我的应用程序中,有一个深色模式,我想知道如何更改状态栏颜色? 谢谢,

UIColor *color = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = color;

可能在AppDelegate.m中的didFinishLaunchingWithOptions方法中的以下代码行可能有所帮助:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

    statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
}

好的,根据Apple Docs setStatusBarStyle:animated: is deprecated since iOS 9.0,你现在应该把这个代码改为:

-(UIStatusBarStyle)preferredStatusBarStyle 
{
return UIStatusBarStyleLightContent;
}

祝你好运,编码愉快::)

在 plist 中添加这些行

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

一种简单有效的方法是在info.plist中添加一个属性,并将状态栏样式设置为UIStatusBarStyleLightContent

这就像一个魅力

我们应该做的是:

  • 首先打开你app的info.plist文件,然后添加一个属性UIViewControllerBasedStatusBarAppearance ,并将其值设置为NO
  • 接下来前往您的Appdelegate或任何类文件并使用以下代码:

     UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;

    另一种编写代码的方法是:

     [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];

斯威夫特 3 解决方案:
在需要非默认状态栏外观的视图控制器中,使用:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

这对我有用。

点击 navigationBar -> style -> Default 然后选择 Translucent 然后在 Bar Tint 下面 -> (选择你想要的任何颜色)

iOS 13 出来后,我们不得不借助status bar的新属性,将下面的代码放在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

if (@available(iOS 13.0, *)) {

        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];

        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor = [UIColor whiteColor];

        }

        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];



    } else {
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];

        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor = [UIColor whiteColor];

        }

    }

这会处理 iOS 13 中发生的崩溃并为其他 iOS 版本设置框架

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM