Swift UI - HostingController 添加了不需要的导航栏

Tra*_*lly 8 ios swift swiftui

我正在尝试将 SwiftUI 集成到我的项目中,目前我正在使用一个故事板,该故事板是通过我的应用程序委托使用以下代码启动的:


_rootNavigiationController = [[UINavigationController alloc] init];
_rootNavigiationController.navigationBarHidden = YES;

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:StoryboardLoginRegister bundle:nil];
BasicInformation *basicInfo = (BasicInformation *)[storyboard instantiateViewControllerWithIdentifier:@"basic-info"];
[self.rootNavigiationController setViewControllers:@[basicInfo]];
Run Code Online (Sandbox Code Playgroud)

所以本质上我的应用程序委托是在 Objective-C 中,而 Windows 根控制器是 UINavigation 控制器。

我的 BasicInformation 类如下所示:

class BasicInfo: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.isNavigationBarHidden = true;
        // Do any additional setup after loading the view.
    }
    
    @IBSegueAction func addSwiftUi(_ coder: NSCoder) -> UIViewController? {
        let BasicInfoUI = BasicInfo_UI();
        let hostingController = UIHostingController(coder: coder, rootView: BasicInfoUI);
        hostingController?.navigationController?.isNavigationBarHidden = true;
        return hostingController;
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
Run Code Online (Sandbox Code Playgroud)

基本信息的 Swift UI 如下:

struct BasicInfo_UI: View {
    @State var username: String = ""
    @State var isPrivate: Bool = true
    @State var notificationsEnabled: Bool = false
    @State private var previewIndex = 0
    var previewOptions = ["Always", "When Unlocked", "Never"]

    var body: some View {
        Form {
            Section(header: Text("PROFILE")) {
                TextField("Username", text: $username)
                Toggle(isOn: $isPrivate) {
                    Text("Private Account")
                }
            }
            
            Section(header: Text("NOTIFICATIONS")) {
                Toggle(isOn: $notificationsEnabled) {
                    Text("Enabled")
                }
                Picker(selection: $previewIndex, label: Text("Show Previews")) {
                    ForEach(0 ..< previewOptions.count) {
                        Text(self.previewOptions[$0])
                    }
                }
            }
            
            Section(header: Text("ABOUT")) {
                HStack {
                    Text("Version")
                    Spacer()
                    Text("2.2.1")
                }
            }
            
            Section {
                Button(action: {
                    print("Perform an action here...")
                }) {
                    Text("Reset All Settings")
                }
            }
        }
    }
}

struct BasicInfo_UI_Previews: PreviewProvider {
    static var previews: some View {
        BasicInfo_UI()
    }
}

Run Code Online (Sandbox Code Playgroud)

我唯一的问题是我似乎无法弄清楚为什么我的应用程序的用户界面顶部有一个导航栏

不需要的导航栏

希望有人可以向我解释为什么我的控制器事件顶部有一个导航栏,尽管我已在应用程序的多个位置明确将 navigationbarhidden 设置为 true

Asp*_*eri 7

尝试通过 SwiftUI 显式隐藏导航栏,例如

Form {

  // ... other code

}
.navigationBarTitle("")
.navigationBarHidden(true)
Run Code Online (Sandbox Code Playgroud)