【问题标题】:Tappable area of Button and Image in SwiftUISwiftUI 中 Button 和 Image 的可点击区域
【发布时间】:2021-03-22 21:31:08
【问题描述】:

    Button(action: {
       print("Round Action")
       }) {
       Text("Press")
       .frame(width: 50, height: 50)
       .foregroundColor(Color.black)
       .background(Color.red)
       .clipShape(Circle())
       }
       .background(Color.blue)

我希望实际的按钮是可点击的,而不是它周围的蓝色方形框,还有什么方法可以让我们摆脱那个框但仍然可以调整那个按钮的大小?

Button Image

【问题讨论】:

标签: swift swiftui


【解决方案1】:

你需要为你的Button设置.contentShape,默认是Rectangle

Button(action: {
    print("Round Action")
}) {
    Text("Press")
        .frame(width: 150, height: 150)
        .foregroundColor(Color.black)
        .background(Color.red)
        .clipShape(Circle())
}
.contentShape(Circle()) // <- here
.background(Color.blue)

有什么方法可以让我们摆脱那个框架但仍然可以调整那个按钮的大小?

您可以尝试使用paddingscaleEffect,但frame 是更改视图宽度和高度的推荐方式。

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2021-09-24
    • 2014-11-11
    相关资源
    最近更新 更多