【问题标题】:SwiftUI Tabview inside TabView and List items highlight behaviour strange?TabView 和 List 项中的 SwiftUI Tabview 突出显示行为奇怪?
【发布时间】:2022-07-05 17:20:20
【问题描述】:

我是Swift的新手,请帮帮我

第一个问题是可以在TabView 中使用TabView 吗?

如果可以,我遇到了与 List 相关的问题,当我点击另一个地方时,List 中的项目会突出显示

// This tab will have many pages and can swap left/right to change tabs
struct Tab1: View {
    var body: some View {
        TabView(selection: $otherSelected) {
            SubOfTab1()
                .tag("tag1")

            ... other tabs
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
    }
}

// This tabview will display tab item as bottom
struct ContentView: View {
    var body: some View {
        TabView(selection: $selected) {
            var body: some View {
                Tab1()
                    .tag("tagParent1")

                ... other tabs
            }
        }
    }
}

struct SubOfTab1: View {
    VStack {
        Text("Try to tab this text")   ------> Try to tab this and all list items below is hightlighted??? why???
            .frame(height: 100)

        List() {
            Text("XX")
            Text("XX")
            Text("XX")
            Text("XX")
            ...
        }
    }
}

第一次运行

点击上面的文字,List中的所有项目都是highlighted??

【问题讨论】:

    标签: swiftui swiftui-tabview


    【解决方案1】:

    我在 TabView 内的 .plain 样式列表存在类似问题,点击外部列表会导致选择所有可见列表项。
    设置列表单元格的 .listRowBackground() 修复了它。
    在你的情况下:

    List() {
            Text("XX")
                .listRowBackground(Color.black)
            Text("XX")
                .listRowBackground(Color.black)
            Text("XX")
                .listRowBackground(Color.black)
            Text("XX")
                .listRowBackground(Color.black)
            ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2021-05-18
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多