2017-02-03 133 views
1

不知何故,无论我遇到的查询或答案的数量如何,我都找不到一个人可以说使用Swift 3在iOS中设置状态栏颜色。我已经看到了所有在您添加的建议:状态栏颜色iOS

override var preferredStatusBarStyle: UIStatusBarStyle { 
    return .lightContent 
} 

但事情是,我不希望有一个透明的状态栏。我想要一个特定的十六进制颜色的状态栏颜色。

+1

“我不希望有一个透明的状态栏” 真倒霉。它是透明的(在最新版本的iOS中)。一旦你接受了现实,你就可以开始接受解决方法了。 – matt

回答

1

没有API设置状态栏内容的颜色;如果你需要一个,你应该提交一个增强请求here。如果你想改变状态栏背景的颜色,在你的应用窗口的顶部放置一个带有背景颜色的视图。

+0

Upvote在状态栏下方放置彩色视图。可悲的是,这是我的做法。 – foxanna

0

这是关于状态栏更改的Apple Guidelines/Instruction。只有黑暗&灯(而&黑色)被允许在状态栏。

这里是 - 如何更改状态栏样式:

如果你想设置状态栏样式,应用的水平,那么设置UIViewControllerBasedStatusBarAppearance在你的`的.plist”文件NO

,如果你婉设置状态栏样式,在视图控制器的水平,那么请按照下列步骤操作:

  1. .plist文件中设置UIViewControllerBasedStatusBarAppearanceYES,如果你需要在只的UIViewController级别设置状态栏样式。
  2. 在viewDidLoad中添加功能 - setNeedsStatusBarAppearanceUpdate

  3. 覆盖preferredStatusBarStyle在您的视图控制器。

-

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.setNeedsStatusBarAppearanceUpdate() 
} 

override var preferredStatusBarStyle: UIStatusBarStyle { 
    return .lightContent 
} 

集的.plist根据状态栏样式设置电平值。 enter image description here


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

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 
    } 

} 



下面是结果:

enter image description here