iOS Custom Status Bar Background Color not displaying

后端 未结 9 1840
别那么骄傲
别那么骄傲 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 13:53

    Edit for Swift 3:

    With UITabBarController

    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)
    

    Without embedded controllers

    I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:

    Add this method in your AppDelegate.swift and call it in the didFinishLaunchingWithOptions

    func customizeAppearance() {
        UINavigationBar.appearance().barTintColor = UIColor.black
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        UITabBar.appearance().barTintColor = UIColor.black
        let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
    
        UITabBar.appearance().tintColor = tintColor
    }
    

    Thanks to @Utsav I added the following subview to my UITabBarController and this seems to be working now:

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

    The UITabBarController doesn't seem to play well in AppDelegate. If anyone has a better way let me know but, as of now this is the solution I have come around to.

提交回复
热议问题