form1.cn
Make a little progress every day

Swift3.0设置状态栏的背景颜色与文字颜色

12th of April 2017 Swift Swift+IOS 6045

设置背景颜色

//定义以下方法:
func setStatusBarBackgroundColor(color : UIColor) {
    let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
    let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = color
    }
}

//在override func viewDidLoad()中调用
self.setStatusBarBackgroundColor(color: .black)


设置文字颜色

第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为 NO

第二步:在viewDidLoad中加一句

UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

这样就可以把默认的黑色改为白色。