简体   繁体   English

根据其下方的内容更改状态栏的颜色

[英]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?我们确实有很多关于将状态栏变为白色的问题,但我想知道如何根据状态栏下方的内容将其变为白色或黑色,与 iOS 9 的音乐应用程序相同,是否可以动态进行此更改使用任何 api 还是我必须手动决定状态栏的颜色?

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 .info.plist分配NO给这个View controller-based status bar appearance then in my app delegate's didFinishLaunchingWithOptions :然后在我的app delegate's didFinishLaunchingWithOptions

UIApplication.shared.statusBarStyle = .lightContent

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.您可以通过以下代码检查 UIColor 是否更暗。

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.如果您得到帮助或对此有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM