从 SwiftUIHostingController 中删除后退按钮

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

这里后退按钮仍然出现,我想将其从第二个视图控制器中删除

final class SwiftUIHostingController<Content>: UIHostingController<Content> where Content: View {

    override init(rootView: Content) {
        super.init(rootView: rootView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationItem.setHidesBackButton(true, animated: animated)
    }

}

struct MyViewOne: View {
    var body: some View {
        Text("Hello, World!")
    }
}

#Preview {
    MyViewOne()
}

struct MyViewTwo: View {
    var body: some View {
        Text("2")
    }
}

#Preview {
    MyViewTwo()
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)
        let navigationController = UINavigationController(rootViewController: SwiftUIHostingController<MyViewOne>(rootView: MyViewOne()))
        navigationController.navigationBar.topItem?.hidesBackButton = true
        navigationController.pushViewController(SwiftUIHostingController<MyViewTwo>(rootView: MyViewTwo()), animated: true)
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()
    }
    }
    ```

swift
1个回答
0
投票

我只是想知道为什么你需要实现这样的东西。但是,您可能想尝试这种方法。然后你就无法再弹回

MyViewOne

struct MyViewTwo: View {
    var body: some View {
        Text("2")
            .navigationBarBackButtonHidden()
    }
}

输出

enter image description here

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