状态栏和导航栏好像颜色不一样iOSSwift

Status bar and navigation bar does not seems to have the same color iOS Swift

我想将状态栏颜色设置为与导航栏颜色相同。当我尝试将导航栏和状态栏设置为相同颜色时,导航栏总是以比状态栏更浅的颜色出现。

这就是我想要的:

我的结果:

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

任何人都可以给我任何关于如何解决这个问题的建议。

提前致谢!

要达到预期效果,请将状态栏样式设置为默认样式并将 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

警告!不要在 iOS 13 或更高版本中执行任何此操作,并且绝对不要在 iOS 15 或更高版本中执行此操作。这是很久以前的过时代码!


我能够得到想要的结果:

这是我使用的代码 (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 或者给它一个背景图像(这是最好地控制它的颜色的方法)。

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

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

根据@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)
    }
}

我也遇到过同样的问题,后来发现不是状态栏的问题,而是导航栏的问题。如果将导航栏的背景颜色和一个简单的 UIView 设置为相同,则显示的真实颜色会有所不同。要使导航栏的颜色与另一个视图的颜色相同:

密码错误

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

正确的代码

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

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

对于像我这样晚到这里的人(Xcode 13.1,iOS 15.1),这可能会有所帮助:

都是关于定义 standardAppearancescrollEdgeAppearance 的外观并设置它们各自的背景颜色。

就个人而言,甚至不需要以编程方式解决此问题: Navigation Bar Attributes