In iOS13 the status bar background colour is different from the navigation bar in large text mode

南笙酒味 提交于 2019-11-27 15:19:55

No hacks or funkiness required here. The key is defining the desired appearance and setting this value on BOTH the nav bar's standardAppearance AND its scrollEdgeAppearance. I have the following in the init for my base navigation controller subclass for my entire app:

if #available(iOS 13.0, *) {     let navBarAppearance = UINavigationBarAppearance()     navBarAppearance.configureWithOpaqueBackground()     navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]     navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]     navBarAppearance.backgroundColor = UIColor.Theme.primary     navigationBar.standardAppearance = navBarAppearance     navigationBar.scrollEdgeAppearance = navBarAppearance } 

If the problem is that you'd like to give the navigation bar a color when the large title is showing, use the new UINavigationBarAppearance class.

    let app = UINavigationBarAppearance()     app.backgroundColor = .blue     self.navigationController?.navigationBar.scrollEdgeAppearance = app 

On iOS 13, navigation bars using large title have a transparent color per Apple human interface guidelines. See more infos here:

In iOS 13 and later, a large title navigation bar doesn’t include a background material or shadow by default. Also, a large title transitions to a standard title as people begin scrolling the content

I've discovered that with storyboards you have to fake the nav bar (only really works with opaque nav bars, assuming your green is opaque). The best way I found was to create a placeholder view (purple) that fits the safe area insets, and then add a fake view behind the nav bar (cyan/blue) that is the height remaining. Works for my project, but yeah it's a bit of a hack.

Edit: This is mainly for LaunchScreen.storyboard where you can't use a custom view controller class.

Thanks to Mike and Hans's answer. My case is half transparent status bar and nav bar with alpha 0.5. iOS13 seems complicated. Below is my test result, will work if you want transparent for both.

if #available(iOS 13.0, *) {                 let navBarAppearance = UINavigationBarAppearance()                 // This only set top status bar as transparent, not the nav bar.                 navBarAppearance .configureWithTransparentBackground()                 // This set the color for both status bar and nav bar(alpha 1).                 navBarAppearance.backgroundColor = UIColor.red.withAlphaComponent(0.5)                 navigationController?.navigationBar.standardAppearance = navBarAppearance                 navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance                 // Nav bar need sets to translucent for both nav bar and status bar to be translucent.                 navigationController?.navigationBar.isTranslucent = true                 // // Need to reset nav bar's color to make it clear to display navBarAppearance's color                 navigationController?.navigationBar.backgroundColor = UIColor.clear                }  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!