【问题标题】:SwiftUI: How to change the image for a selected item in TabbedViewSwiftUI:如何更改 TabbedView 中所选项目的图像
【发布时间】:2019-07-22 06:45:22
【问题描述】:

有没有什么方法可以改变 SwiftUI 的TabbedView 中的标签项在被选中或未选中时的图像?

TabbedView(selection: $selection) {
  Text("Home").tabItem {
    Image(systemName: "house")
    Text("Home")
  }.tag(0)

  Text("Away").tabItem {
    Image("away")
    Text("Away")
  }.tag(1)
}

我尝试在网上搜索,但没有找到答案。 我正在使用 Xcode 11 beta 4。

【问题讨论】:

    标签: ios swiftui xcode11


    【解决方案1】:

    您可以使用条件/三元运算符并根据$selection 渲染图像

    参见示例:

    struct ContentView: View {
        @State private var selection = 0
    
        var body: some View {
            TabView(selection: $selection) {
                Text("Home")
                    .tabItem {
                        selection == 0 ? Image(systemName: "house.fill") : Image(systemName: "house")
                        Text("Home")
                    }
                    .tag(0)
    
                Text("Away")
                    .tabItem {
                        selection == 1 ? Image(systemName: "a.circle.fill") : Image(systemName: "hand.raised.fill")
                        Text("Away")
                    }
                    .tag(1)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      相关资源
      最近更新 更多