1

I'm trying to change the barTintColor from SceneDelegate, but it does not cover the whole top area. take a look at my ss:

enter image description here

How can I make it covers the entire top area?

Here's my code, thank you in advance.

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }

    UINavigationBar.appearance().barTintColor = UIColor(displayP3Red: 47/225, green: 54/225, blue: 64/225, alpha: 1.0)
    UINavigationBar.appearance().backgroundColor = UIColor(displayP3Red: 47/225, green: 54/225, blue: 64/225, alpha: 1.0)
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
}
}
ios coder
  • 1
  • 4
  • 31
  • 91
Aldo Sugiarto
  • 197
  • 3
  • 20

2 Answers2

2

So, apparently it's because the iOS version. Pre iOS 13 can work with just

UINavigationBar.appearance().barTintColor = .yellow

But now it's a bit different. SO to this guy, i found the answer here

So what I do now is:

    let appearance = UINavigationBarAppearance()
    appearance.backgroundColor = .red

    UINavigationBar.appearance().standardAppearance = appearance // for scrolling bg color
    UINavigationBar.appearance().compactAppearance = appearance // not sure why it's here, but u can remove it and still works
    UINavigationBar.appearance().scrollEdgeAppearance = appearance // for large title bg color

that is at least work for me.

Aldo Sugiarto
  • 197
  • 3
  • 20
0

This should work in just one line:

UINavigationBar.appearance().barTintColor = .yellow
Alastar
  • 1,284
  • 1
  • 8
  • 14