SwiftUI圆形按钮在单击时变形

比约恩·莫瑞(Bjorn Morrhaye)

我用以下代码创建了一个圆形按钮:

struct RoundButton : View {

    var value = "..."

    var body: some View {
        GeometryReader { geometry in
            Button(action: {}) {
                VStack {
                    Text(self.value)
                        .font(.headline)
                        .color(Color("NightlyDark"))
                    Text("MIN")
                        .font(.footnote)
                        .color(.white)
                }
                .frame(width: geometry.size.width, height: geometry.size.height)
            }
            .clipShape(Circle())
        }
    }
}

但是,单击按钮时,形状会变形。知道为什么会这样吗?

在此处输入图片说明

当我使用时也会发生同样的情况 .mask(Circle())

这是测试版内容还是正常行为?有谁知道创建圆形按钮的更好方法吗?

凯坦·奥德拉

Button默认情况下,这里发生的是将所有屏幕[宽+高]视为其框架。

所以你必须设置Button也。

我认为这是 watchOS

这是您的代码:

struct RoundButton: View {

    var value = "..."

    var body: some View {

        GeometryReader { geometry in

            Button(action: {}) {
                VStack {
                    Text(self.value)
                        .font(.headline)
                        .foregroundColor(Color("NightlyDark"))
                    Text("MIN")
                        .font(.footnote)
                        .foregroundColor(.white)
                }
                .frame(width: geometry.size.width, height: geometry.size.height)
            }
            .frame(width: geometry.size.width, height: geometry.size.height)
                .clipShape(Circle())
        }

    }
}

注意:我正在使用Xcode 11 Beta 4

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章