SwiftUI:如何增加可以触发按钮的区域? [英] SwiftUI: How can I increase the area in which a button can be triggered?

查看:45
本文介绍了SwiftUI:如何增加可以触发按钮的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不改变 UI 的情况下增加可以触发按钮的区域?

How can I increase the area in which a button can be triggered without changing the UI?

这是我的代码

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Text")

            .navigationBarTitle(Text("Title"))
            .navigationBarItems(
                leading:
                Button(action: { print("add") }) {
                    Image(systemName: SFSymbolName.plus)
                        .font(.system(size: 18))
                }
            )
        }
    }
}

推荐答案

对于这种特殊情况,您可以为除前缘之外的所有边添加填充到按钮的label:

For this particular situation, You can add padding to all edges excluding the leading edge to the label of the button:

Button(action: { print("add") }) {
    Text("+")
        .padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 50))
}
.background(Color.red) // This is just for seeing the hit area. You should get rid of it

注意最大可点击区域应该在标题上方的矩形内:

Note that The maximum tappable area should be inside the rectangle above the title:

这篇关于SwiftUI:如何增加可以触发按钮的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆