如何在 SwiftUI 中增加 navigationBarItem 的可点击区域?

0rt*_*0rt 4 ios swift swiftui

我有这个代码 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)
    }
} 
Run Code Online (Sandbox Code Playgroud)

resetButton 看起来像这样: 在此处输入图片说明

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

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

Moj*_*ini 12

您可以更改按钮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)
}
Run Code Online (Sandbox Code Playgroud)