When pushing from a SwiftUI view to a UIKit, navigation bar items are not present or not added.
I have added one item in the storyboard and one item in code, neither show up.
This is possibly a bug in SwiftUI, but I feel like this is probably a pretty common case and perhaps I am missing something?
There is some overlap with this question: Navigation Bar Items after push from SwiftUI to UIKit although I am not sure if they are the same or not
Results
Inputs
- No code, Storyboard only
- No code, Storyboard only
class MyHostingController: UIHostingController<SwiftUIView> {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder, rootView: SwiftUIView())
}
}
struct SwiftUIView: View {
var body: some View {
NavigationLink(
destination: MyUIKitView(),
label: {
Text("Go to MyUIKitView")
})
}
}
Glue between 3 & 4
struct MyUIKitView: UIViewControllerRepresentable {
typealias UIViewControllerType = MyUIKitViewController
func makeUIViewController(context: Context) -> MyUIKitViewController {
let sb = UIStoryboard(name: "Main", bundle: nil)
let myVC = sb.instantiateViewController(identifier: "MyUIKitViewController") as! MyUIKitViewController
return myVC
}
func updateUIViewController(_ uiViewController: MyUIKitViewController, context: Context) {
print("??")
}
}
class MyUIKitViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// ?? ADD NAV BUTTON HERE
navigationItem.rightBarButtonItems?.append(UIBarButtonItem(systemItem: .camera))
}
}
- No code, Storyboard only
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…