iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😶‍🌫️

[SwiftUI] Is the List's tap area too narrow? How to make the margins tappable too

に公開1件

Introduction

As shown in the attached GIF, when only the text or icons of a List can be tapped, the user feels that the tap area is narrow and the operation experience is poor.
タップ範囲が狭い状態

Therefore, in this article, we will set up the List so that blank areas are also tappable along with the text and icons.

Improvement Points

In SwiftUI, blank areas like HStack and Spacer may have a visual appearance but no tap recognition.

Therefore, it is necessary to add the following adjustments:

  • Expand the layout with HStack and Spacer()
  • Explicitly define the tap area with .contentShape(Rectangle())

Code Before Improvement

First, the code where only the text or icons of the List can be tapped is as follows.
In this state, blank areas other than text and icons cannot be tapped.

Full code before improvement
import SwiftUI

struct ListView: View {
    @State private var isAccountInfoActive = false

    var body: some View {
        NavigationView {
            ZStack {
                Color(UIColor.systemGroupedBackground)
                    .ignoresSafeArea()

                VStack(spacing: 20) {
                    List {
                        Section {
                            // Navigation link (hidden) + onTapGesture
                            ZStack {
                                SettingsRow(icon: "person.circle.fill", iconColor: .blue, text: "アカウント情報")
                                    .onTapGesture {
                                        isAccountInfoActive = true
                                    }

                                NavigationLink(destination: AccountInfoView(), isActive: $isAccountInfoActive) {
                                    EmptyView()
                                }
                                .hidden()
                            }

                            SettingsRow(icon: "envelope.circle.fill", iconColor: .pink, text: "お問い合わせ")
                            SettingsRow(icon: "doc.text.fill", iconColor: .orange, text: "利用規約")
                            SettingsRow(icon: "doc.plaintext.fill", iconColor: .gray, text: "ライセンス情報")
                        }
                    }
                    .listStyle(InsetGroupedListStyle())
                    .frame(height: 300)

                    // Logout button
                    Button(action: {
                        print("ログアウト tapped")
                    }) {
                        Text("ログアウト")
                            .foregroundColor(.blue)
                            .frame(maxWidth: .infinity)
                            .padding()
                            .background(Color(UIColor.secondarySystemGroupedBackground))
                            .cornerRadius(10)
                            .shadow(radius: 1)
                    }
                    .padding(.horizontal)

                    Spacer()
                }
                .navigationTitle("設定")
            }
        }
    }
}

struct SettingsRow: View {
    let icon: String
    let iconColor: Color
    let text: String
    var onTextTap: (() -> Void)? = nil

    var body: some View {
        HStack {
            Image(systemName: icon)
                .foregroundColor(.white)
                .frame(width: 30, height: 30)
                .background(iconColor)
                .cornerRadius(5)

            if let onTextTap = onTextTap {
                Text(text)
                    .onTapGesture {
                        onTextTap()
                    }
            } else {
                Text(text)
            }

            Spacer()
        }
        .padding(.vertical, 5)
    }
}

struct AccountInfoView: View {
    var body: some View {
        Text("アカウント情報画面")
            .font(.title)
            .navigationTitle("アカウント情報")
            .navigationBarTitleDisplayMode(.inline)
    }
}

#Preview {
    ListView()
}

Code After Improvement

By adding the following modifications, the List rows become tappable including blank areas.

Full code after modification
import SwiftUI

struct ListView: View {
    @State private var isAccountInfoActive = false

    var body: some View {
        NavigationView {
            ZStack {
                Color(UIColor.systemGroupedBackground)
                    .ignoresSafeArea()

                VStack(spacing: 20) {
                    List {
                        Section {
                            // Navigation link (hidden) + onTapGesture
                            ZStack {
                                // Wrap SettingsRow in HStack and expand width with Spacer
                                HStack {
                                    SettingsRow(icon: "person.circle.fill", iconColor: .blue, text: "アカウント情報")
                                    Spacer() // Ensures space extends to the right edge
                                }
                                .contentShape(Rectangle()) // Expands tap recognition to the entire row
                                .onTapGesture {
                                    isAccountInfoActive = true
                                }

                                NavigationLink(destination: AccountInfoView(), isActive: $isAccountInfoActive) {
                                    EmptyView()
                                }
                                .hidden()
                            }

                            SettingsRow(icon: "envelope.circle.fill", iconColor: .pink, text: "お問い合わせ")
                            SettingsRow(icon: "doc.text.fill", iconColor: .orange, text: "利用規約")
                            SettingsRow(icon: "doc.plaintext.fill", iconColor: .gray, text: "ライセンス情報")
                        }
                    }
                    .listStyle(InsetGroupedListStyle())
                    .frame(height: 300)

                    // Logout button
                    Button(action: {
                        print("ログアウト tapped")
                    }) {
                        Text("ログアウト")
                            .foregroundColor(.blue)
                            .frame(maxWidth: .infinity)
                            .padding()
                            .background(Color(UIColor.secondarySystemGroupedBackground))
                            .cornerRadius(10)
                            .shadow(radius: 1)
                    }
                    .padding(.horizontal)

                    Spacer()
                }
                .navigationTitle("設定")
            }
        }
    }
}

struct SettingsRow: View {
    let icon: String
    let iconColor: Color
    let text: String
    var onTextTap: (() -> Void)? = nil

    var body: some View {
        HStack {
            Image(systemName: icon)
                .foregroundColor(.white)
                .frame(width: 30, height: 30)
                .background(iconColor)
                .cornerRadius(5)

            if let onTextTap = onTextTap {
                Text(text)
                    .onTapGesture {
                        onTextTap()
                    }
            } else {
                Text(text)
            }

            Spacer()
        }
        .padding(.vertical, 5)
    }
}

struct AccountInfoView: View {
    var body: some View {
        Text("アカウント情報画面")
            .font(.title)
            .navigationTitle("アカウント情報")
            .navigationBarTitleDisplayMode(.inline)
    }
}

#Preview {
    ListView()
}

Seeing the Differences

Let's look closely at what changed.
↓before

List {
    Section {
        ZStack {
            SettingsRow(icon: "person.circle.fill", iconColor: .blue, text: "アカウント情報")
                .onTapGesture {
                    isAccountInfoActive = true
                }
            
            NavigationLink(destination: AccountInfoView(), isActive: $isAccountInfoActive) {
                EmptyView()
            }
            .hidden()
        }
        
        SettingsRow(icon: "envelope.circle.fill", iconColor: .pink, text: "お問い合わせ")
        SettingsRow(icon: "doc.text.fill", iconColor: .orange, text: "利用規約")
        SettingsRow(icon: "doc.plaintext.fill", iconColor: .gray, text: "ライセンス情報")
    }
}

↓after

List {
    Section {
        ZStack {
            // Wrap SettingsRow in HStack and expand width with Spacer
+           HStack {
                SettingsRow(icon: "person.circle.fill", iconColor: .blue, text: "アカウント情報")
+               Spacer() // Ensures space extends to the right edge
+           }
+           .contentShape(Rectangle()) // Expands tap recognition to the entire row
            .onTapGesture {
                isAccountInfoActive = true
            }
            
            NavigationLink(destination: AccountInfoView(), isActive: $isAccountInfoActive) {
                EmptyView()
            }
            .hidden()
        }
        
        SettingsRow(icon: "envelope.circle.fill", iconColor: .pink, text: "お問い合わせ")
        SettingsRow(icon: "doc.text.fill", iconColor: .orange, text: "利用規約")
        SettingsRow(icon: "doc.plaintext.fill", iconColor: .gray, text: "ライセンス情報")
    }

You can confirm that the following code has been added:

List {
    HStack {
        Spacer()
    }
    .contentShape(Rectangle())
}

Explanation: Why Does This Expand the Tap Area?

① The Meaning of HStack + Spacer()

For an explanation of HStack, FuyanTech's article is easy to understand.
https://zenn.dev/fuya777/books/67a40f438c9c56/viewer/9508ed

When arranging multiple elements on the screen, we use a mechanism called "stack."
Without "stacks," we would have to place each element individually, which would be very cumbersome.

HStack is a method of arranging elements horizontally.

The image is as follows:

Inside HStack {}, we can see that icon, text, and space() are grouped together.

HStack {
    SettingsRow(icon: "person.circle.fill", iconColor: .blue, text: "アカウント情報")
    Spacer()
}

In this case, by including space(), the width is formed including the blank area.

Before HStack After HStack

② The Role of .contentShape(Rectangle())

.contentShape() is a modifier that specifies the "tappable hit-test area."

In SwiftUI, HStack and Spacer often have a visual size but no tap recognition.

By adding .contentShape(Rectangle()), the entire area can be recognized as one large tap region.

As a result of the expanded tap area, we achieved a UI that responds smoothly even in blank areas, as shown below:

Summary

  • The combination of HStack + Spacer + .contentShape(Rectangle()) makes the entire row tappable.
  • Even small improvements can significantly enhance UX.

Discussion

ビスタービスター

NavigationView(すでに非推奨です)ではなくNavigationStackを利用し、NavigationLinkのみを書くようにすればこの不具合は既に解消されています!

1