Change Status Bar Background Color in Swift 3

前端 未结 14 1631
挽巷
挽巷 2020-11-28 04:21

In XCode 7.3.x ill changed the background Color for my StatusBar with:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = UIApplicatio         


        
14条回答
  •  不知归路
    2020-11-28 04:48

    "Change" status bar background color:

    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)
    

    Change status bar text color:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    Update: please note that the status bar frame will change when the view is rotated. You could update the created subview frame by:

    • Using the autoresizing mask: statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
    • Observing NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
    • Or overriding viewWillLayoutSubviews()

提交回复
热议问题