Reputation: 7644
I have a parent view where I want to navigation title to be large. Then I push a child view with a grouped table view and I want to navigation title to be inline
:
This works well with SwiftUI but when my parent is a UIViewController with prefersLargeTitles = true
and I push a UIHostingController(rootView: ChildView())
the navigation bar title in the child jumps
Here is the expected behaviour (using SwiftUI only)
Here is the behaviour when pushing from UIKit:
This is the SwiftUI code that works
struct ParentView: View {
var body: some View {
NavigationView {
Text("SwiftUI")
.navigationBarItems(trailing: navigationButton)
.navigationBarTitle("SwiftUI Parent", displayMode: .large)
}
}
var navigationButton: some View {
NavigationLink(destination: ChildView()) {
Text("Push")
}
}
}
struct ChildView: View {
let list = ["One", "Two", "Three"]
var body: some View {
List(list, id: \.self, rowContent: Text.init)
.listStyle(GroupedListStyle())
.navigationBarTitle("SwiftUI Child", displayMode: .inline)
}
}
struct ChildView_Previews: PreviewProvider {
static var previews: some View {
ChildView()
}
}
And the is how I push the SwiftUI child from UIKit
class ParentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "UIKit Parent"
let barItem = UIBarButtonItem(title: "Push", style: .plain, target: self, action: #selector(pushChild))
navigationItem.rightBarButtonItem = barItem
view.backgroundColor = UIColor.systemBackground
navigationController?.navigationBar.prefersLargeTitles = true
}
@objc
func pushChild(_ sender: Any) {
let childViewController = UIHostingController(rootView: ChildView())
navigationController?.pushViewController(childViewController, animated: true)
}
}
Upvotes: 2
Views: 892
Reputation: 543
Set child view controller's navigationItem.largeTitleDisplayMode
to .never
before pushing it(even it is UIHostingController).
func pushChild(_ sender: Any) {
let childViewController = UIHostingController(rootView: ChildView())
childViewController.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(childViewController, animated: true)
}
Upvotes: 2