首页 > 解决方案 > 如何在 SwiftUI 中增加 navigationBarItem 的可点击区域?

问题描述

我有这个代码view

struct ContentView: View {
    var body: some View {
        NavigationView{
            List{
                ForEach(0...5, id: \.self) { note in
                    VStack(alignment: .leading) {
                        Text("title")
                        Text("subtitle")
                            .font(.subheadline)
                            .foregroundColor(.secondary)
                    }
                }
            }
            .navigationBarItems(trailing: resetButton)
            .navigationBarTitle(Text("Notes"))
        }
    }

    var resetButton: some View {
        Button(action: {
            print("reset")
        }) {
            Image(systemName: "arrow.clockwise")
        }
        .background(Color.yellow)
    }
} 

resetButton看起来像这样: 在此处输入图像描述

当我点击 时resetButton,似乎只有黄色区域对触摸有反应。

如何使此按钮的可点击区域更大?(让它表现得像一个正常的UIBarButtonItem

标签: iosswiftswiftui

解决方案


您可以更改按钮view 内部的框架:

var resetButton: some View {
    Button(action: {
        print("reset")
    }) {
        Image(systemName: "arrow.clockwise")
        .frame(width: 44, height: 44) // Or any other size you like
    }
    .background(Color.yellow)
}

推荐阅读