我想看看是否可以根据选择的选项卡项更改底部 tabview 的颜色。目前,我可以使用 init.d 中的以下代码清除 tabview 栏。
let tabBar = UITabBar.appearance()
init() {
tabBar.barTintColor = UIColor.clear
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage()
}
...
TabView(selection: $selectedTab) {
FirstView()
.tabItem{
Text("First")
}
SecondView()
.tabItem{
Text("Second")
}
}
.onAppear{
setTabViewBackground()
}
func setTabViewBackground() {
if selectedTab != 0 {
tabBar.barTintColor = UIColor.blue
}
}
Run Code Online (Sandbox Code Playgroud)
试图在身体重绘和 idk 时触发 func,如果它的这种声明式风格得到了我的最好,但根本不改变 tabview 背景。
使用 SwiftUI,我有几个嵌套在 TabBar 内的 NavigationView。这样做的原因是我想更改每个 NavigationView 的标题以反映所选选项卡,但我找不到其他方法来执行此操作。另外,对于我的客户来说,UITabBar 的背景颜色为纯白色非常重要。为此我设置了UITabBar.appearance().isTranslucent = false
,否则它显示为灰色。然而,一旦我这样做,我就会在 UITabBar 上方看到一条奇怪的灰线。我怎样才能摆脱这个?
struct ContentView: View {
init() {
UITabBar.appearance().backgroundColor = UIColor.white
UITabBar.appearance().isTranslucent = false
}
var body: some View {
TabView {
NavigationView {
Text("First tab")
.padding(10)
.background(Color.white)
.navigationBarTitle(Text("First tab"), displayMode: .inline)
}
.tabItem {
Text("First tab")
}
NavigationView {
Text("Second tab")
.padding(10)
.background(Color.white)
.navigationBarTitle(Text("Second tab"), displayMode: .inline)
}
.tabItem {
Text("Second tab")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)