状态栏和导航栏在iOS Swift中似乎没有相同的颜色

4
我希望将状态栏的颜色设置为与导航栏相同的颜色。当我尝试将导航栏和状态栏设置为相同的颜色时,导航栏总是比状态栏呈现出较浅的颜色。
这就是我想要的:

enter image description here

我的结果:

enter image description here

在AppDelegate中的代码:

状态栏:

UIApplication.sharedApplication().statusBarStyle = .Default
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector(Selector("setBackgroundColor:")) 
{
      statusBar.backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
      statusBar.tintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
}

导航栏:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        UINavigationBar.appearance().backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UIApplication.sharedApplication().statusBarHidden = false
        UIApplication.sharedApplication().statusBarStyle = .Default

有人能给我提供解决这个问题的建议吗?

先行致谢!


1
将视图的颜色设置为与导航栏相同的颜色,这样就可以实现你想要的效果。 - Hosny
这并不像你在导航栏上使用 whiteColor 作为色调,而在状态栏上使用 UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0) 那么简单。 - StudioTime
请更新到Swift 3。 - matt
是的,那就是下一步,目前版本是Swift 2.3。升级到Swift 3.0后,一些功能不再起作用,这就是我将其留在Swift 2.3的原因。但我们已经忙于修复问题了。由于我们需要尽快提交它,这是唯一的解决方案,只能将其保留在Swift 2.3中。 - Melchior
6个回答

8

警告!不要在iOS 13或更高版本上执行此操作,尤其不要在iOS 15或更高版本上执行。这是很久以前的过时代码!


我成功地得到了期望的结果:

enter image description here

这是我使用的代码(Swift 3):

let app = UINavigationBar.appearance()
// nav bar color => your color
app.barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
app.isTranslucent = false
// status bar text => white
app.barStyle = .black
// nav bar elements color => white
app.tintColor = .white
app.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]

代码中的这行是非法的,很可能会导致您的应用被禁止在App Store中上架:

let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView

对于状态栏,iOS现在的颜色是透明的。你不需要设置它的颜色,也不应该这样做。导航栏在状态栏后面延伸,因此它们的颜色永远是相同的,因为你看到的始终是同一个界面对象,即导航栏。
至于设置导航栏的颜色,请记住它默认情况下是半透明的。如果不想让它透明,你不能准确地设置它的颜色。而且你不应该设置它的backgroundColor。你应该设置它的barTintColor或者使用背景图像(这是获得最好控制它颜色的方法)。

浏览其他论坛时,我发现使用那段代码是我可以改变状态栏颜色的唯一方法。因为在某些部分,状态必须以不同的颜色出现。 - Melchior

4

对我来说,关键是要更改isTranslucent属性:

navigationBar.isTranslucent = false
navigationBar.barTintColor = PlateColors.mainRed
navigationBar.backgroundColor = PlateColors.mainRed

1

1

根据@matt的建议,在状态栏下扩展导航栏,导航栏必须是不透明的:

    let navigationBar = navigationController?.navigationBar
    navigationBar?.isOpaque = true
    navigationBar?.setBackgroundImage(UIImage(color: UIColor(white: 0, alpha: 0.5)), for: .default)
    navigationBar?.shadowImage = UIImage()

extension UIImage {

    convenience init?(color: UIColor) {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        context?.setFillColor(color.cgColor)
        context?.fill(rect)

        let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage)
    }
}

1
为了达到所需的效果,请将状态栏样式设置为默认,并将UINavigationBar.appearance().barTintColor设置为所需的颜色。
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
UIApplication.sharedApplication().statusBarStyle = .Default

谢谢,使用您的代码我得到了完全符合预期的结果! - Melchior

0

我遇到了同样的问题,发现这不是状态栏的问题,而是导航栏的问题。如果你将导航栏和一个简单的UIView设置为相同的背景颜色,实际显示的颜色将是不同的。要使导航栏的颜色与另一个视图相同:

错误代码

navigationBar.backgroundColor = .blue
view.backgroundColor = .blue

正确的代码

navigationBar.isTranslucent = false
navigationBar.barTintColor = .blue
view.backgroundColor = .blue

在这种情况下,它们的颜色将是相同的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接