Search code examples
iosswiftxcodeswiftuiuikit

Check if there is navigation, and present and push from a SwiftUI view


I am experimenting with SwiftUI. I already have a UIKit based app working, and I want to integrate a SwiftUI view. I achieve to show this SwiftUI view by using UIHostingController.

In this SwiftUI, I intercept a button action. In this action, I want to:

  1. Check if there is a navigation controller (what it used to be self.navigationController on UIKit)
  2. Be able to present or push (through self.navigationController) a new UIKit view controller from this SwiftUI view.

I cannot find any way to achieve any of these 3 things on SwiftUI


Solution

  • You could do this multiple ways: delegates, closures or heck even with combine publishers. The easiest way to get started is with an action closure I think. It could look something like this:

    struct SwiftUIView: View {
        let action: () -> Void
        
        var body: some View {
            Button("press me", action: action)
        }
    }
    
    class ViewController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let swiftUIView = SwiftUIView(action: handleButtonPress)
            
            let hostingController = UIHostingController(rootView: swiftUIView)
            addChild(hostingController)
            
            hostingController.view.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(hostingController.view)
     
            NSLayoutConstraint.activate([
                hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
                hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
                hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
                hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
            ])
        }
        
        func handleButtonPress() {
            print("TODO: Insert navigation controller logic here")
        }
    }