ios – How one can apply Content material ViewController background to backside SafeArea in UINavigationController?

0
3
ios – How one can apply Content material ViewController background to backside SafeArea in UINavigationController?


Whereas porting my current UIKit based mostly app to SwiftUI I battle with a easy activity:

A UIViewControllerRepresentable holds a UINavigationController.

The UINavigationController shows VCs with completely different background colours. These should not utilized to the underside secure space. How can I alter this, in order that the secure space has the colour of the lively little one VC?

enter image description here

struct SomeSheetContent: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        let firstVC = UIViewController()
        firstVC.view.backgroundColor = .inexperienced
        firstVC.title = "First VC"
        firstVC.edgesForExtendedLayout = [.all]
        firstVC.extendedLayoutIncludesOpaqueBars = true

        let button = UIButton(sort: .system)
        button.setTitle("Subsequent", for: .regular)
        button.titleLabel?.font = .systemFont(ofSize: 20, weight: .daring)
        button.addTarget(context.coordinator, motion: #selector(Coordinator.didTapButton), for: .touchUpInside)

        firstVC.view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: firstVC.view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: firstVC.view.centerYAnchor)
        ])
        

        let navVC = UINavigationController(rootViewController: firstVC)
        navVC.navigationBar.isTranslucent = false
        navVC.edgesForExtendedLayout = [.all]
        
        context.coordinator.navigationController = navVC
        
        return navVC
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }

    class Coordinator: NSObject {
        let guardian: SomeSheetContent
        weak var navigationController: UINavigationController?

        init(_ guardian: SomeSheetContent) {
            self.guardian = guardian
        }

        @objc func didTapButton() {
            let secondVC = UIViewController()
            secondVC.view.backgroundColor = .orange
            secondVC.title = "Second VC"
            secondVC.edgesForExtendedLayout = [.all]
            secondVC.extendedLayoutIncludesOpaqueBars = true

            navigationController?.pushViewController(secondVC, animated: true)
        }
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here