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

时间:2019-11-23 20:43:59

标签: 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)
    }
} 

resetButton看起来像这样: enter image description here

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

如何使此按钮的可点击区域更大? (使其表现得像普通的UIBarButtonItem

2 个答案:

答案 0 :(得分:3)

您可以更改按钮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)
}

答案 1 :(得分:2)

This blog post为我指明了正确的方向,我需要直接在图片上添加一些填充。

enter image description here

Button(action: {
    // Triggered code
}) {
    Image(systemName: "plus")
        .font(.system(size: 22, weight: .regular))
        .padding(.vertical)
        .padding(.leading, 60)
}
.background(Color.red) // Not needed