xcode 如何在SwiftUI iOS中从视图呈现视图控制器时隐藏导航栏

oknrviil  于 2023-06-24  发布在  Swift
关注(0)|答案(1)|浏览(140)

我有一个导航到视图控制器的视图(视图控制器被 Package 在UIViewControllerRepresentable中)。
视图控制器有自己的导航栏。但是当我导航到屏幕时,导航栏会被放置在后退按钮的下面。
如何将视图控制器的导航栏和后退按钮显示在同一栏中?

var body: some View {
        NavigationView(content: {
            NavigationLink {
                SimpleViewRepresentable()
            } label: {
                Text("Present View Controller")
            }
        })
    }
}

class SimpleViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44))
        view.addSubview(navBar)
        navBar.backgroundColor = .red
        self.navigationItem.title = "Title"
    }
}

struct SimpleViewRepresentable: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UINavigationController {
        let vc =  UINavigationController(rootViewController: SimpleViewController())
        vc.setNavigationBarHidden(false, animated: true)
        return vc
    }
    
    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
    
}

omqzjyyz

omqzjyyz1#

我认为你需要修改一些代码,比如:

var body: some View {
    NavigationView(content: {
        NavigationLink {
            SimpleViewRepresentable().navigationBarHidden(true)
        } label: {
            Text("Present View Controller")
        }
    })
}

请检查并让我知道如果它不工作在你的身边。

相关问题