SwiftUI can't tap in Spacer of HStack

前端 未结 7 498
北海茫月
北海茫月 2020-12-28 12:08

I\'ve got a List view and each row of the list contains an HStack with some text view(\'s) and an image, like so:

HStack{
    Text(group.name)
    Spacer()
          


        
相关标签:
7条回答
  • 2020-12-28 12:52

    Simple extension based on Jim's answer

    extension Spacer {
        /// https://stackoverflow.com/a/57416760/3393964
        public func onTapGesture(count: Int = 1, perform action: @escaping () -> Void) -> some View {
            ZStack {
                Color.black.opacity(0.001).onTapGesture(count: count, perform: action)
                self
            }
        }
    }
    

    Now this works

    Spacer().onTapGesture {
        // do something
    }
    
    0 讨论(0)
提交回复
热议问题