【问题标题】:Multiple ContextMenu in swiftUI Form/List rowswiftUI表单/列表行中的多个ContextMenu
【发布时间】:2020-11-07 10:42:51
【问题描述】:

是否可以将具有两个不同contextMenu 的两个对象放在Form/List 的同一行中?

例子:

List {
  HStack {
    Image("image.A").contextMenu{ Text("I'm A") }
    Image("image.B").contextMenu{ Text("I'm B") }
  }
}

类似问题:SwiftUI - Multiple Buttons in a List row

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    这是一种可能的方法。使用@State 区分情况并使用onLongPressGesture 进行修改

    在 iOS 13.5 上测试

    struct ContentView: View {
        
        @State var whichImage: Int = 0
        
        var body: some View {
            List {
              HStack {
                Image("image1").resizable().frame(width: 40, height: 40).onLongPressGesture {
                    self.whichImage = 1
                }
                Image("image2").resizable().frame(width: 40, height: 40).onLongPressGesture {
                    self.whichImage = 2
                }
                
              }.contextMenu{ Text(String(self.whichImage)) }
            }
        }
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2022-06-10
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多