SwiftUI 中另一个按钮内的按钮

Iva*_*van 12 button ios swift swiftui

如何让内部Button执行其操作而不是始终执行外部操作?

struct Foo: View {
    var body: some View {
        Button(action: { print("alloha") }) {
            HStack {
                Button(action: { print("hello") }) {
                    Image(systemName: "circle.fill")
                }
                Text("world")
                Spacer()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 1

它可以工作,只需为内部按钮提供足够的空间,因此点击测试可以指定要激活的按钮,如下例所示。(使用 Xcode 11.4 / iOS 13.4 测试)

演示

struct Foo: View {
    var body: some View {
        Button(action: { print("alloha") }) {
            HStack {
                Button(action: { print("hello") }) {
                    Image(systemName: "circle.fill").padding() // << more space !!
                        .border(Color.red) // << for testing
                }
                Text("world")
                Spacer()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你好,@Asperi 有没有办法在按下内部按钮时禁用外部按钮的按下状态? (19认同)