SwiftUI:按钮的圆角边缘边框尺寸加倍。

huangapple go评论111阅读模式
英文:

SwiftUI: Border is double in size at the rounded edges of button

问题

我在按钮上得到了一个双重边框,看起来很奇怪。我尝试使用RoundRectangle叠加创建形状,但得到了相同的结果。这是我使用的代码:

ScrollView(.horizontal, showsIndicators: false) {
    HStack {
        Button(action:{ action()}) {
            Label("Name", image: "info")
        }
        .frame(height: 40)
        .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
        .foregroundColor(.green)
        .background(.clear)
        .overlay(
            Capsule(style: .continuous)
            .stroke(.green, lineWidth: 1)
        )}
}

结果是:

SwiftUI:按钮的圆角边缘边框尺寸加倍。

英文:

I get a double border on a button and it looks strange. I tried to create the shape with RoundRectangle overlay, and got the same result. This is the code I use:

    ScrollView(.horizontal, showsIndicators: false) {
        HStack {
            Button(action:{ action()}) {
                   Label("Name", image: "info")
               }
            .frame(height: 40)
            .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
            .foregroundColor(.green)
            .background(.clear)
            .overlay(
                Capsule(style: .continuous)
                .stroke(.green, lineWidth: 1)
            )}}

And the result is:
SwiftUI:按钮的圆角边缘边框尺寸加倍。

答案1

得分: 2

以下是翻译好的部分:

问题不在于边框在两侧是双倍粗,而在于顶部和底部只有一半的粗度。按钮被“ScrollView”裁剪了。

在这种情况下,您可以通过向您的“HStack”添加填充来解决这个问题:

之前:

SwiftUI:按钮的圆角边缘边框尺寸加倍。

向“HStack”添加.padding([.top, .bottom], 1)

SwiftUI:按钮的圆角边缘边框尺寸加倍。

英文:

The problem isn't that the border is double thickness at the sides, it's that it's half thickness at the top and bottom. The button is being clipped by the ScrollView.

In this case you can fix it by adding a padding to your HStack:

Before:

SwiftUI:按钮的圆角边缘边框尺寸加倍。

Adding .padding([.top, .bottom], 1) to HStack:

SwiftUI:按钮的圆角边缘边框尺寸加倍。

huangapple
  • 本文由 发表于 2023年2月6日 17:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定