swift - SwiftUI 中的圆角边框

标签 swift swiftui

如何在 SwiftUI 中圆形边框?

我认为这会起作用:

.cornerRadius(10)
.border(Color.white)

它不起作用。

这是我现在的解决方法:
.overlay(RoundedRectangle(cornerRadius: 10).stroke(lineWidth: 1).foregroundColor(.white))

最佳答案

这不是解决方法,而是您在 SwiftUI 中的处理方式。两件事情:

  • 曾经有一个 cornerRadius在……beta 4 中被弃用的修饰符?测试版 5?是的,它一直是一个移动的目标。
  • 很棒 非常感谢@kontiki ( blog post ),这是一个很好地返回你想要的扩展:
     extension View {
         public func addBorder<S>(_ content: S, width: CGFloat = 1, cornerRadius: CGFloat) -> some View where S : ShapeStyle {
             let roundedRect = RoundedRectangle(cornerRadius: cornerRadius)
             return clipShape(roundedRect)
                  .overlay(roundedRect.strokeBorder(content, lineWidth: width))
         }
     }
    

  • 用法:
    .addBorder(Color.white, width: 1, cornerRadius: 10)
    

    关于swift - SwiftUI 中的圆角边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753997/

    相关文章:

    ios - 重新加载表格 View 的选定单元格

    ios - 使用来自 RxSwift 变量的 searchBar 过滤 collectionView

    ios - 返回错误的结果用于在 swift 中打印 FIRST 和 LAST DATE OF THE MONTH

    在SwiftUI中使用字符串插值进行本地化

    arrays - 如何使用搜索栏对字典数组进行排序

    swift - 谷歌标记聚类。如何将此库添加到项目中? swift

    ios - 从 SceneDelegate 更新屏幕结构的状态

    ios - SwiftUI 无法删除选择器上方的空间 - 表单版本

    ios - 如何更改整个应用程序的 tintColor? swift 用户界面

    swift - 将 SwiftUI 颜色转换为 UIColor