SwiftUIのListでSpacerの部分にもタップ判定をつける


SwiftUIのListでSpacerの部分にもタップ判定をつける方法です。

SwiftUIのListでSpacerの部分にもタップ判定をつける

ContentView.swift

import SwiftUI

struct ContentView: View {
    let pokemons: [String] = ["Snorlax", "Pikachu", "Slowpoke"]
    var body: some View {
        List(pokemons, id: \.self) {pokemon in
            HStack {
                Text(pokemon)
                Spacer()
            }
            .contentShape(Rectangle())
            .onTapGesture {
                print(pokemon)
            }
        }
    }
}

#Preview {
    ContentView()
}