在 SwiftUI 中单击选项卡栏项时更改选项卡栏背景颜色

时间:2021-07-10 17:44:23

标签: swift swiftui swiftui-tabview

我有一个小型 SwiftUI 项目。当我单击第一个标签栏时,我想将标签栏背景颜色更改为黑色,当我单击零标签栏项目时,我想将背景颜色更改为白色。有什么想法吗?

截图:

enter image description here

内容视图:

struct ContentView: View {

  init() {
    UITabBar.appearance().barTintColor = .systemBackground
    UITabBar.appearance().unselectedItemTintColor = UIColor(named: "TabBarUnselected")
  }
  
  var body: some View {
    TabView {
      
      Text("Zero")
        .tabItem {
          Label("Zero", systemImage: "0.square.fill")
        }
      
      Text("One")
        .tabItem {
          Label("One", systemImage: "1.square.fill")
        }
    }
    .accentColor(Color("TabBarTint"))
  }
}

0 个答案:

没有答案