在Swift 3中更改状态栏背景颜色

问题描述 投票:45回答:10

在XCode 7.3.x中,我使用以下命令更改了StatusBar的背景颜色:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
    return
}
statusBar.backgroundColor = color
}

但似乎这不再适用于Swift 3.0。

我尝试过:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
    return
}
statusBar.backgroundColor = color
}

但它给了我:

this class is not key value coding-compliant for the key statusBar.

任何想法如何使用XCode8 / Swift 3.0进行更改?

ios swift swift3 statusbar uistatusbar
10个回答
140
投票
extension UIApplication {
    var statusBarView: UIView? {
        if responds(to: Selector("statusBar")) {
            return value(forKey: "statusBar") as? UIView
        }
        return nil
    }
}

UIApplication.shared.statusBarView?.backgroundColor = .red

0
投票

在扩展程序文件中添加以下代码,以便在swift 4及更高版本中编辑状态栏:

extension UIApplication {

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

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}

现在,我们可以通过在ViewController类中添加以下行来编辑状态栏:

let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
let backview = UIView(frame: frame)

backview.backgroundColor = backgroundColor
screenController.view.addSubview(backview)
screenController.view.bringSubviewToFront(backview)

希望,这会有所帮助。谢谢


0
投票

适用于Swift 4+和iOS 13+的解决方案

extension UIApplication {
    var statusBarView: UIView? {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
        return nil
    }
}

UIApplication.shared.statusBarView?.backgroundColor = <Your Color name> ex: UIApplication.shared.statusBarView?.backgroundColor = .red


48
投票

“更改”状态栏背景颜色:

let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)

更改状态栏文字颜色:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

更新:请注意,旋转视图时状态栏框架将更改。您可以通过以下方式更新创建的子视图框架:

  • 使用自动调整掩码:statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
  • 观察NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
  • 或者压倒viewWillLayoutSubviews()

28
投票

使用Swift 3和4,您可以使用下面的代码片段。它使用UIApplication找到来自valueForKeyPath的视图设置它的背景颜色。

guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
        return
    }
statusBarView.backgroundColor = UIColor.red

Objective-C的

UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:@"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
    statusBarView.backgroundColor = [UIColor redColor];
}

6
投票

对于Xcode 9和iOS 11:我们将尝试实现的状态栏的样式是具有白色内容的状态栏。转到ViewController.swift文件并添加以下代码行。

override var preferredStatusBarStyle: UIStatusBarStyle {

     return .lightContent

}

或者从项目设置选项中,您可以更改状态栏的样式:

enter image description here

接下来,返回故事板,选择View Controller,然后在Editor菜单中选择Embed in Navigation Controller。选择导航栏,然后在“属性”检查器中将“条形”颜色设置为红色。故事板将如下所示。 enter image description here

构建并运行项目,状态栏的内容再次变暗,这是默认值。原因是,iOS要求导航控制器的状态栏的样式而不是包含的视图控制器。

enter image description here

要更改应用程序内所有导航控制器的样式,请在AppDelegate.swift文件中更改以下方法。

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UINavigationBar.appearance().barStyle = .blackOpaque
    return true
    }

再次构建并运行项目,这次状态栏的内容变为白色。

enter image description here


5
投票

对于iOS 11和Xcode 9,请使用以下步骤。

  1. 创建UIApplication类的扩展: qazxsw poi
  2. 在您的班级或您想要更改状态栏背景颜色的任何位置: qazxsw poi
  3. 对于状态栏的轻量级内容或暗内容,只需转到Info.plist并添加值为NO的以下值行。

查看基于控制器的状态栏外观

  1. 现在只需在项目设置的“常规”选项卡中设置光照内容或任何需要的内容。

3
投票

试试这个

转到你的应用info.plist

  1. 将基于控制器的状态栏外观设置为NO
  2. 将状态栏样式设置为UIStatusBarStyleLightContent

然后转到您的应用程序委托并将以下代码粘贴到您设置Windows的RootViewController的位置。

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

2
投票
UIApplication.shared.statusBarView?.backgroundColor = .red

这对我有用,因为我的导航栏TintColor是黑色的,无法看到状态栏。

当设置上面的代码时,它确实将FininLaunch状态栏显示为白色。


1
投票

您可以在应用程序启动期间或视图控制器的viewDidLoad期间为状态栏设置背景颜色。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

结果如下:


0
投票

可能的解决方案是在viewController上添加将用作statusBar背景的视图:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ?) -> Bool {
    // Override point for customization after application launch.

    UINavigationBar.appearance().barStyle = .blackOpaque
    return true
}
© www.soinside.com 2019 - 2024. All rights reserved.