如何在iOS中更改状态栏文本颜色

1087

我的应用程序有一个黑色的背景,但在iOS 7中状态栏变成透明的了。因此我什么都看不到,只能看到角落里的绿色电池指示器。我该如何将状态栏文本颜色更改为像主屏幕上那样的白色呢?


查看我的答案以获取更好的解决方案 https://dev59.com/OmMm5IYBdhLWcg3wX98W#65367444 - Ucdemir
Xcode似乎在不断变化,因此我建议向下滚动以查找更近期的解决方案(例如,截至2021年的超级简单答案)。 - Eric33187
60个回答

2

iOS 17+ 更新

鉴于应用程序下方的情况,并且操作系统自动根据其自身修改状态栏样式。


1
请尝试这个。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [application setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor blackColor];
    }

1

我需要为Swift和导航控制器做些什么

extension UINavigationController {
    override open var preferredStatusBarStyle: UIStatusBarStyle {
       return .lightContent
    }   
}

1
这个解决方案适用于使用新的SwiftUI生命周期/iOS 14.0的应用程序:
我需要动态更改状态栏文本颜色,但无法访问window.rootViewController,因为SceneDelegate在SwiftUI生命周期中不存在。
最后,我找到了Xavier Donnellon的简单解决方案:https://github.com/xavierdonnellon/swiftui-statusbarstyleStatusBarController.swift文件复制到您的项目中,并将主视图包装到RootView中:
@main
struct ProjectApp: App {     
    var body: some Scene {
        WindowGroup {
            //wrap main view in RootView
            RootView {
                //Put the view you want your app to present here
                ContentView()
                    //add necessary environment objects here 
            }
        }
    }
}

然后,您可以使用.statusBarStyle(.darkContent).statusBarStyle(.lightContent)视图修饰符来更改状态栏文本颜色,或者直接调用例如UIApplication.setStatusBarStyle(.lightContent)

不要忘记在Info.plist中将“基于视图控制器的状态栏外观”设置为“是”。


1
在iOS 7上,如果您想使用UIViewControllerBasedStatusBarAppearance == YES,并且您的根视图控制器是UINavigationController,您应该对其进行子类化并重载childViewControllerForStatusBarStyle,例如,像这样:
- (UIViewController*) childViewControllerForStatusBarStyle
{
    return self.viewControllers.lastObject;
}

在此之后,将调用推送的视图控制器上的preferredStatusBarStyle方法。


1
你可以在iOS 6和7上使用这个:
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif

[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];

0
如果您想将其设置为任何颜色,请使用以下代码。
id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];

SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
    // iOS 7+
    [statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
                                                                 ^^^^^^^^^^^^^
}

我知道我正在访问私有API,但我在许多项目中使用过它,而且苹果已经批准了。

只需在提交应用程序时,在评论部分将此代码发送给苹果,并告知您正在使用此代码以更改状态栏颜色。

是的也不要忘记下面这个。


那我该如何在Swift中实现呢? - אורי orihpt

0

这里有一个更好的解决方案,扩展导航控制器并将其放入故事板中。

class NVC: UINavigationController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
              return .lightContent
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    self.navigationBar.isHidden = true
    self.navigationController?.navigationBar.isTranslucent = false
      
     self.navigationBar.barTintColor = UIColor.white
     setStatusBarColor(view : self.view)
    }
    

    func setStatusBarColor(view : UIView){
             if #available(iOS 13.0, *) {
                 let app = UIApplication.shared
                 let statusBarHeight: CGFloat = app.statusBarFrame.size.height
                 
                 let statusbarView = UIView()
              statusbarView.backgroundColor = UIColor.black
                 view.addSubview(statusbarView)
               
                 statusbarView.translatesAutoresizingMaskIntoConstraints = false
                 statusbarView.heightAnchor
                     .constraint(equalToConstant: statusBarHeight).isActive = true
                 statusbarView.widthAnchor
                     .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
                 statusbarView.topAnchor
                     .constraint(equalTo: view.topAnchor).isActive = true
                 statusbarView.centerXAnchor
                     .constraint(equalTo: view.centerXAnchor).isActive = true
               
             } else {
                 let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
              statusBar?.backgroundColor = UIColor.black
             }
         }
}

状态栏颜色将为黑色,文字将为白色


做得好,我尊重你的解决方案,但我不会向任何人推荐这个解决方案。我看到很多开发者在代码中添加一个新类并使用那个包装器类(主要是为标签和导航控制器编写)。在我的建议中,我们不应该编写包装器类来实现此功能,我们总是使用扩展或使用本地行为来解决这种问题。希望你不介意我的意见。谢谢。 - Muhammad Danish Qureshi

-2
extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

你能解释一下为什么这个有效吗? - economy

-4
非常简单的方法来更改状态栏颜色。 创建导航控制器的子类。
在viewDidLoad方法中编写此代码。 将此代码应用于所有视图控制器。
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
                                                                        [UIColor whiteColor],
                                               NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};

3
这个问题是关于状态栏而不是导航栏的。 - Lescai Ionel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接