admaDIC App Development & IT Solutions

Mixing UIKit and SwiftUI with UIHostingController

by Annett Schwarze | 2026-05-01

A practical pattern for incrementally migrating UIKit view controllers to SwiftUI without breaking existing navigation involves using `UIHostingController`. Based on our Music Theory Learning app, this example demonstrates passing navigation context and handling two-way communication between UIKit and SwiftUI.

        
@objc
class TopicSelectionNGViewController: UIViewController {
    let appModel: AppModel = AppModel.shared
    private var hostingController: UIHostingController < TopicSelectionView >?

    private func hostSwiftUIView() {
        if hostingController != nil { return }

        let rootView = TopicSelectionView(
            navigationController: self.navigationController,
            hostingViewController: self,
            appModel: appModel
        )
        let hosting = UIHostingController(rootView: rootView)
        hosting.view.translatesAutoresizingMaskIntoConstraints = false

        addChild(hosting)
        view.addSubview(hosting.view)
        NSLayoutConstraint.activate([
            hosting.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            hosting.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            hosting.view.topAnchor.constraint(equalTo: view.topAnchor),
            hosting.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        hosting.didMove(toParent: self)
    }
}
    
UIHostingController

 

www.admadic.de | webmaster@admadic.de | Legal Notice and Trademarks | Privacy
© 2005-2007 - admaDIC | All Rights Reserved
All other trademarks and/or registered trademarks are the property of their respective owners
Last Change: Fri May 1 09:15:58 2026 GMT