从 SwiftUI 推送到 UIKit 后的导航栏项目

问题描述 投票:0回答:2

我有一个 SwiftUI

NavigationView
,我在其中推送到
UIViewControllerRepresentable
符合
UIViewController
。导航视图自然会添加后退按钮。但是,我在推送视图控制器中设置的所有导航栏按钮也消失了。

添加项目的代码并不是什么新鲜事,如果我从一个好的 UIKit 视图控制器中推送,效果就很好:

navigationItem.rightBarButtonItem = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(filterItemTapped(button:)))

我的理论是他们没有在 NavigationView 和 UIViewController 的导航项之间建立桥接,但也许有人知道解决方法。

swift uinavigationcontroller uinavigationbar swiftui
2个回答
0
投票

这是我刚刚使用的简单方法,效果非常好。

struct SwiftUIView: View {
    var body: some View {
        NavigationLink(
            destination: ,
            label: {
                Text("Go to MyUIKitView")
            }
        )
    }

    @ViewBuilder
    func destinationView() -> some View {
        MyUIKitView()
            .navigationTitle("Your Title")
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button(
                        action: { 
                            // Do something here
                        },
                        label: { Image(systemName: "camera") }
                    )
                }
            }
    }
}

-3
投票

在您的

UIViewControllerRepresentable
中,用
UIViewController
包裹您的
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) {

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.