如何解决不透明的UITabBar创建奇怪的灰色条
使用SwiftUI,我在TabBar内嵌套了几个NavigationViews。这样做的原因是,我想更改每个NavigationView的标题以反映所选的选项卡,而我找不到其他方法来做到这一点。同样,对UITabBar的背景颜色为纯白色对我的客户也非常重要。因此,我设置了files.forEach(el => {
if (el.name.includes(fName)) {
res.write(el.name + el.ext);
} else {
console.log('nothing')
}
});
res.end()
,否则它显示为灰色。但是,一旦这样做,我就会在UITabBar上方看到一条奇怪的灰色线。我该如何摆脱呢?
UITabBar.appearance().isTranslucent = false
解决方法
此
UITabBar.appearance().isTranslucent = false
是一种hack(有关TabView内部实现的未记录假设),它破坏了NavigationView
的布局(删除了NavigationView具有活动约束的视图)
以下是可能的解决方法:
- 仅使用一个根
NavigationView
struct ContentView: View {
init() {
UITabBar.appearance().backgroundColor = UIColor.white
UITabBar.appearance().isTranslucent = false
}
@State private var title = ""
var body: some View {
NavigationView {
TabView {
Text("First tab")
.padding(10)
.background(Color.white)
.onAppear {
self.title = "First tab"
}
.tabItem {
Text("First tab")
}
Text("Second tab")
.padding(10)
.background(Color.white)
.onAppear {
self.title = "Second tab"
}
.tabItem {
Text("Second tab")
}
}
.navigationBarTitle(Text(title),displayMode: .inline)
}
}
}
- 创建自定义标签栏(使用
HStack
视图中的Button
)
作为Asperi回答中选项的另一种选择,您可以替换以下代码行:
UITabBar.appearance().backgroundColor = UIColor.white
UITabBar.appearance().isTranslucent = false
带有这些:
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithOpaqueBackground()
tabBarAppearance.backgroundColor = UIColor.white
UITabBar.appearance().standardAppearance = tabBarAppearance
它具有相同的效果,但是使用configureWithOpaqueBackground()
而不是将isTranslucent
设置为false可以保留NavigationView所依赖的约束。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。