Search code examples
iosswift3uistatusbar

change color of status bar according to content beneath it


We do have so many question here on turning status bar to white color but I wanna know how can I turn it to white or black according to content beneath the status bar same as iOS 9's music app does, is it possible to make this change dynamically with any api or do I have to manually decide the color of status bar?

This is how I'm turning it to white now (which is not what i want):

In info.plist assigning NO to this View controller-based status bar appearance. then in my app delegate's didFinishLaunchingWithOptions :

UIApplication.shared.statusBarStyle = .lightContent

Solution

  • You have to decide manually status bar color. You can check for is background of status bar is darker then show light content style.

    You can check UIColor is darker or not by below code.

    extension UIColor
    {
        func isLight() -> Bool
        {
            let components = CGColorGetComponents(self.CGColor)
            let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
    
            if brightness < 0.5
            {
                return false
            }
            else
            {
                return true
            }
        }
    }
    

    Let me know if you get help or have any query on the same.