Home > Back-end >  Swift: Status bar color different from Navigation bar color
Swift: Status bar color different from Navigation bar color

Time:04-09

The statusbar on the top seems to take the view background color and not the navigation bar background color. Help would be appreciated

enter image description here

override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Hello"
        self.navigationController?.navigationBar.backgroundColor = .white
        self.view.backgroundColor = .blue
    }

CodePudding user response:

You should use UINavigationBarAppearance to customise the appearance of a navigation bar, instead of changing the background colour of the views directly.

https://developer.apple.com/documentation/uikit/uinavigationbarappearance

You can either set these directly on the UINavigationBar (standardAppearance, compactAppearance, scrollEdgeAppearance, compactScrollEdgeAppearance) or on your view controller's navigation item.

var appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
  • Related