【问题标题】:SwiftUI A blank tabbar height area on the top of tab barSwiftUI 标签栏顶部的空白标签栏高度区域
【发布时间】:2022-01-22 19:37:57
【问题描述】:

我想在UITabBarController 中插入一个新选项卡,它是UIHostingController 的 SwiftUI 视图,就像这张图片:

视图很简单,

ContentView.swift

struct ContentView: View {
       var body: some View {
            ZStack {
                Color(.red).opacity(0.2).edgesIgnoringSafeArea(.all)
                NavigationView {
                    Text("Hello")
                }
            }
}
    

AppDelegate.swift(禁用场景)我得到viewControllers 并像这样插入:

let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "TabBarController") as! UITabBarController
let view = ContentView()
let viewController = UIHostingController(rootView: view)
controller.viewControllers?.insert(viewController, at: 0)
let item = controller.tabBar.items![0]
item.title = "Tab"
window.rootViewController = controller
self.window = window
window.makeKeyAndVisible()

结果如上图,没问题。但是当我将Text 放入NavigationView 时,就像:

var body: some View {
        ZStack {
            Color(.red).opacity(0.2).edgesIgnoringSafeArea(.all)
            NavigationView {
                Text("Hello")
            }
        }
}

那么结果是:

有一个灰色的空白栏,看起来和那里的tabbar一样,如何删除它?

我尝试通过以下方式隐藏标签栏:

tabBarController.tabBar.isHidden = true

是的,它已经消失了,但我不想隐藏 tabBar。任何帮助,谢谢!

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

我终于通过删除故事板中的UITabBarController 并重新添加它来解决。旧的 Xcode 版本添加了旧的。我比较了两个UITabBarControllers,在界面生成器中没有看到区别。

【讨论】:

    【解决方案2】:

    只需将 TabBar 的半透明设置为 true。

    在情节提要中:

    设置复选标记TabBar -> Style -> Translucent

    或在代码中

    tabBar.isTranslucent = true
    

    【讨论】: