gpt4 book ai didi

ios - Swiftui - UIHostingController 添加不需要的导航栏

转载 作者:行者123 更新时间:2023-12-01 21:23:23 31 4
gpt4 key购买 nike

我正在尝试将 SwiftUI 集成到我的项目中,我目前正在使用一个 Storyboard,该 Storyboard是通过我的应用委托(delegate)使用以下代码启动的:


_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 delegate 在 objective-c 中,windows 根 Controller 是一个 UINavigation Controller 。

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

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

Unwanted navigation bar

希望有人能向我解释为什么我的 Controller 事件顶部确实有一个导航栏,尽管我已经在我的应用程序的多个位置明确地将 navigationbarhidden 设置为 true

最佳答案

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

Form {

// ... other code

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

关于ios - Swiftui - UIHostingController 添加不需要的导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63652728/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com