largeTitleDisplayMode doesn't work when using UIHostingController

When using a UIHostingController to host my SwiftUI content, I can't get the navigation title to start large and shift to inline as the user scrolls. Is this a known problem with using the UIHostingController?

    var viewModel: DashboardViewModel!
    lazy var contentView = UIHostingController(rootView: DashboardView(viewModel: viewModel))

    override func viewDidLoad() {
        super.viewDidLoad()
        addChild(contentView)
        view.addSubview(contentView.view)
        setupConstraints()
        navigationItem.title = "Test Title"
        navigationItem.largeTitleDisplayMode = .automatic
    }

The title in the navigation controller shows inline

Post not yet marked as solved Up vote post of MegaWatt Down vote post of MegaWatt
1.3k views

Replies

You need to specify that the navigation bar can display large titles, like this:

navigationController?.navigationBar.prefersLargeTitles = true
  • That shows the large title, but it just stays in-place while the content scrolls under and past the entire nav bar 😞

    It's like the nav bar is placed over the HostingController and the content doesn't know it's there.

  • Does DashboardView have its own navigation bar? As in it's wrapped in a NavigationView (or similar) and sets its own title.

  • No. It's using the UIViewController stack

Try changing the part in viewDidLoad to this:

addChild(contentView)
view.addSubview(contentView.view)
contentView.didMove(toParent: self) // add this
  • Sorry, that didn't help. 😞

Add a Comment

You can set it directly on the UINavigationController()

  let dashBoardNavigationController = UINavigationController(rootView: DashBoardView())
  dashBoardNavigationController.navigationBar.prefersLargeTitles = true

I'm also seeing this issue. Frustrating.