我试图为按钮设置圆形边框,但按钮边框不正确.

代码:

Button(action: {
        print("sign up bin tapped")          
}) {
    Text("SIGN UP")
     .frame(minWidth: 0, maxWidth: .infinity)
      .font(.system(size: 18))
      .padding()
      .foregroundColor(.white)
 }
  .border(Color.white, width: 2)
  .cornerRadius(25)

输出:

enter image description here

正如你所见,拐角处的边界被切断了.

有什么建议我做错了什么?

推荐答案

试着这样做:不要为按钮设置拐角半径,而是为内部视图使用覆盖:

编辑:如果你有一个按钮的背景,你还需要将cornerRadius应用于背景.

    Button(action: {
        print("sign up bin tapped")
    }) {
        Text("SIGN UP")
            .frame(minWidth: 0, maxWidth: .infinity)
            .font(.system(size: 18))
            .padding()
            .foregroundColor(.white)
            .overlay(
                RoundedRectangle(cornerRadius: 25)
                    .stroke(Color.white, lineWidth: 2)
        )
    }
    .background(Color.yellow) // If you have this
    .cornerRadius(25)         // You also need the cornerRadius here
    

对我有用.如果有帮助,请告诉我!

Swift相关问答推荐

防止NavigationLink自行返回导航视图

SwiftUI正在初始化不带状态变量的绑定变量

在SwiftUI中,我如何确保带有符号的单词不会被拆分为两行?

在扩展中创建通用排序函数

如何使用Swift宏和@Observable和@Environment?

当变量首次用于其自己的初始化时,如何清除变量...在初始化之前使用错误?

一组可散列的自定义元素插入相等的元素

在主要参与者中启动分离任务和调用非隔离函数之间的区别

符合协议 - 错误?

P384 公钥获取IncorrectParameterSize

带有屏幕参数的 NSWindow 初始化程序在初始化时导致致命错误

Swift 2.0 方法不能标记为@objc,因为参数的类型不能在 Objective-C 中表示

是否可以在 Swift 中创建通用闭包?

Swift 2 中的新 @convention(c):我该如何使用它?

SwiftUI - 呈现工作表后导航栏按钮不可点击

为 UIImagePicker 设置委托返回错误

修复 Swift 3 中的警告C-style for Statement is deprecated

在 Swift 中覆盖 UIScrollView 的委托属性(就像 UICollectionView 一样)

Swift 2.0 按属性对对象数组进行排序

如何使用 Swift 将文本文件逐行加载到数组中?