set custom color of status bar in swift?

夙愿已清 提交于 2019-12-12 16:03:09

问题


On a view controller, i want to set the color of status bar black but I am not able to change it. I am using below code for this purpose.

 func setUpUI() {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
    }

I have also added the value in info.plist


回答1:


You just override the return value of the status bar, like this:

override var preferredStatusBarStyle: UIStatusBarStyle {
   return .lightContent
}



回答2:


Use below code for swift 4 and Xcode 9.2.

1) In

info.plist Set

View controller-based status bar appearance Key to

NO

2) In

AppDelegate

didFinishLaunchingWithOptions

set Below Code

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.red
    }
    UIApplication.shared.statusBarStyle = .lightContent


来源:https://stackoverflow.com/questions/47172368/set-custom-color-of-status-bar-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!