首页 > 解决方案 > 在 SwiftUI Xcode beta 5 中为图像添加带有cornerRadius 的边框

问题描述

如何cornerRadius向图像添加带有 a 的边框。我收到一条弃用警告,说我应该使用 RoundedRectange 形状,但我不知道如何准确地使用它

测试版 4:

Image(uiImage: ...)
   .border(Color.black, width: 2, cornerRadius: 10)

标签: swiftiphoneswiftuibetaxcode11

解决方案


SwiftUI 1.0

使用cornerRadius 和覆盖修改器

这是另一种我们可以使用cornerRadius修改器(它剪切视图)然后用颜色覆盖笔划的方法。

VStack(spacing: 40) {
    Text("Image Border").font(.largeTitle)
    Text("Using cornerRadius & overlay").font(.title).foregroundColor(.gray)
    Text("Using cornerRadius will also clip the image. Then overlay a border.")
        .frame(minWidth: 0, maxWidth: .infinity)
        .font(.title)
        .padding()
        .background(Color.orange)
        .foregroundColor(.black)

    Image("profile")
        .cornerRadius(10)
        .overlay(RoundedRectangle(cornerRadius: 10)
            .stroke(Color.orange, lineWidth: 4))
        .shadow(radius: 10)
}

结果

在此处输入图像描述


推荐阅读