Neelesh
Neelesh

Reputation: 3693

Swift: Status bar color different from Navigation bar color

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
    }

Upvotes: 2

Views: 2170

Answers (1)

adeasismont
adeasismont

Reputation: 285

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

Upvotes: 4

Related Questions