从 SwiftUI 推送到 UIKit 后的导航栏项目 [英] Navigation Bar Items after push from SwiftUI to UIKit
问题描述
我有一个 SwiftUI NavigationView
,我在其中推送到符合 UIViewController
的 UIViewControllerRepresentable
.导航视图自然会添加后退按钮.但是,我在推到视图控制器中设置的所有导航栏按钮也不见了.
I have a SwiftUI NavigationView
in which I push to a to UIViewControllerRepresentable
conforming UIViewController
. The navigation view naturally adds its back button. However, all navigation bar buttons that I set in the pushed-to view controller are also gone.
用于添加项目的代码并不新鲜,如果我从一个好的 UIKit 视图控制器推送,它就可以正常工作:
The code for adding the item is nothing new and works just fine if I push from a good ol' UIKit View Controller:
navigationItem.rightBarButtonItem = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(filterItemTapped(button:)))
我的理论是他们没有在 NavigationView 和 UIViewController 的导航项之间建立桥接,但也许有人知道一种解决方法.
My theory is that they haven't wired up bridging between NavigationView and UIViewController's navigation items, but maybe somebody knows a workaround.
推荐答案
在你的 UIViewControllerRepresentable
中,用 UINavigationController
包裹你的 UIViewController
.
In your UIViewControllerRepresentable
, wrap your UIViewController
with a UINavigationController
.
struct CustomUIViewControllerRepresentation: UIViewControllerRepresentable {
typealias UIViewControllerType = UINavigationController
func makeUIViewController(context: Context) -> UINavigationController {
let viewController = CustomUIViewController()
let navigationController = UINavigationController(rootViewController: viewController)
return navigationController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
}
}
这篇关于从 SwiftUI 推送到 UIKit 后的导航栏项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!