iOS Custom Status Bar Background Color not displaying

后端 未结 9 1816
别那么骄傲
别那么骄傲 2020-12-16 13:30

I am trying to fill the status bar background color to orange using the following

UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigation         


        
相关标签:
9条回答
  • 2020-12-16 14:07

    This is how I did it without adding a view in a VC with in a NavBarController

    I wanted the color of the status bar to be the same as the VC view color so I just wrote:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.grayColor()
        self.navigationController?.navigationBar.clipsToBounds = true
    }
    

    Try it.

    0 讨论(0)
  • 2020-12-16 14:16

    Simon's answer in swift 3

    let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
    view.backgroundColor = .orange
    self.view.addSubview(view)
    

    There is one other way I know which uses private api. This has some benefits when orientation changes and keyboard is presented and view move up. I've used it and was lucky every time (app was released in the app store).

    func setStatusBarBackgroundColor(color: UIColor) {
    
        guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
    
        statusBar.backgroundColor = color
    } 
    
    0 讨论(0)
  • 2020-12-16 14:18

    UINavigationBar.appereance() works for upcoming viewControllers, but not the currently displayed rootViewController. To achieve this I have added the following to my didFinishLaunchingWithOptions:

    UINavigationBar.appearance().tintColor = myColor
    
    let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
    navigationController.navigationBar.barTintColor = myColor
    navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
    navigationController.navigationBar.translucent = false
    
    navigationController.setNeedsStatusBarAppearanceUpdate()
    
    0 讨论(0)
提交回复
热议问题