ios - 如何指定或更改按钮 SwiftUI 的可点击区域

标签 ios swift button swiftui

我刚刚进入 SwiftUI 的世界,所以我不知道这个问题是否有一个非常明显的答案,但基本上我已经创建了一个按钮,并试图让按钮成为两个同心圆。然而,一旦我在模拟器上运行我的代码,我发现我可以点击屏幕上的任何地方,并且按钮将被“按下”(我使用打印语句对其进行了测试)。我想将按钮的可点击区域限制在圆圈的区域或者可能是两个圆圈的正方形区域。
这是我的代码:

    Button(action: {
    print("Do Something")

}) {
    
    Circle().stroke(Color.white, lineWidth: 3)
        .frame(width: 65, height: 65)
        .position(x: x, y: y)
        .overlay(
            Circle().stroke(Color.white, lineWidth: 3)
                .frame(width: 55, height: 55)
                .position(x: x, y: y)
    )

}
这是我在实时预览窗口中单击它时的样子:
enter image description here

最佳答案

您应该删除 .position修饰符,因为它将 View 位置(在您的情况下为按钮内容)切换到全局坐标。
而是使用堆栈布局,如下所示

    VStack {
        Spacer()          // << pushes button down
        Button(action: {
            print("Do Something")

        }) {

            Circle().stroke(Color.white, lineWidth: 3)
                .frame(width: 65, height: 65)
                .overlay(
                    Circle().stroke(Color.white, lineWidth: 3)
                        .frame(width: 55, height: 55)
                )

        }
    }

关于ios - 如何指定或更改按钮 SwiftUI 的可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63286695/

相关文章:

vb.net - 在 vb.net 中动态更改按钮背景颜色

html - 如何创建自己的 Like 按钮? (与 Facebook 无关)

button - Corona SDK - "ShowOverlay"与 LetterBox 问题

ios - 3D 触摸到 UITabView

python - PyAPNS SSL3_WRITE_PENDING 错误

ios - 确定哪个 segue 触发了 UIViewController

swift - Cordova错误: Promise rejected with non-error: 'Error code 70 for command:

ios - 如何在 Objective-C 中转换以下 Swift 代码?

ios - 如何让按钮根据按下的行显示不同的视频?

swift - 无效 session token (代码 : 209 Version: 1. 7.4)