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

kxxlusnw  于 2023-01-12  发布在  Swift
关注(0)|答案(3)|浏览(236)

我正在尝试将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]];

因此,本质上我的App委托位于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.
    }
    */

}

基本信息的Swift界面如下:

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()
    }
}

我唯一的问题是,我似乎不明白为什么我的应用程序UI的顶部有一个导航栏

希望有人能给我解释一下为什么在我的控制器事件的顶部会有一个导航栏,尽管我已经在应用程序的多个地方显式地将navigationbarhidden设置为true

68bkxrlz

68bkxrlz1#

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

Form {

  // ... other code

}
.navigationBarTitle("")
.navigationBarHidden(true)
vx6bjr1n

vx6bjr1n2#

经过长时间的尝试,我在UIKit项目和SwiftUI视图中使用UIHostingController的行为与仅使用UIKit的行为相同。
在我的故事板中,UIHostingController嵌入在NavigationController中,而NavigationController又连接到UITabBarController
要做的第一件事是取消选中NavigationController的属性检查器中的“显示导航栏”。
在SwiftUI视图中,我在带有.navigationBarTitle("ViewTitle", displayMode: .large)修饰符的NavigationView中有列表,在下一个SwiftUI视图中没有NavigationView,在列表中有.navigationBarTitle ("SecondViewTitle", displayMode: .inline)修饰符。

rt4zxlrg

rt4zxlrg3#

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    setNavigationBarHidden(true, animated: false)
}

这对我有用。

相关问题