bg color of view controller status bar area in UIViewControllerRepresentable

373 views Asked by At

I am using a UIViewControllerRepresentable for a view controller that can accomplish things I can't do in SwiftUI. I am trying to get the status bar area appear the same color as the dark blue, but it persists in being white. I tried earlier to embed it in a navigation controller, but that didn't worked either. (Ignore the Table and View beneath it in the hierarchy on the left, I have them hidden while I try to figure out what is going on with the colors at top). Any ideas how I might force the top area to conform to the desired color?

Thank you.

enter image description here

EDITED TO ADD: My code in my UIViewControllerRepresentable:

    func makeUIViewController(context: Context) -> UINavigationController {
        let storyboard = UIStoryboard(name: "RoundEntry", bundle: nil)
        
        let roundEntryVC = storyboard.instantiateViewController(identifier:
                "RoundEntry") as! RoundEntryVC
        let navigationController  = UINavigationController(rootViewController: roundEntryVC)
        return navigationController
    }

Here's my attempt to change the navbar color in the RoundEntry VC (in viewDidLoad):

        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = UIColor(.orange)
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
        navigationController?.navigationBar.tintColor = .white
1

There are 1 answers

0
tsr On

You might be looking for

.edgesIgnoringSafeArea(.top)

where your UIViewControllerRepresentable is used in SwiftUI.

It worked in my case where I was also embedding container view controller. There could be some side effects I'm not aware of so please play with it.