【问题标题】:Add a side bar to a list of items to skip to the alphabet selected in SwiftUI将侧栏添加到项目列表以跳到 SwiftUI 中选择的字母表
【发布时间】:2020-03-18 23:36:49
【问题描述】:

我不确定实际的术语是什么,但请看屏幕截图。

我指的是列表右侧的 A、B、C、D、E、...。

有人知道如何在 SwiftUI 中实现吗?

【问题讨论】:

    标签: list swiftui


    【解决方案1】:

    自己动手做! (复制-粘贴-测试)示例

    import SwiftUI
    
    struct ContentView: View {
        @State var sel: Int = 0
        let arr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".map(String.init)
        var body: some View {
            VStack(alignment: .leading) {
                Text("selected: " + arr[sel]).padding().border(Color.red)
                HStack(alignment: .top) {
    
                    // place holder
                    Text(arr[sel]).font(.largeTitle).frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.yellow.opacity(0.1))
    
                    VStack {
                        ForEach(0 ..< arr.count) { (i) in
                            Text(self.arr[i]).onTapGesture {
                                self.sel = i
                            }.foregroundColor(Color.blue).padding(.trailing, 5)
                        }
                    }
                }
                Spacer()
            }
        }
    }
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2011-06-18
      • 2021-08-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多