如何添加類似于以下附件的圓形視圖。在附件中,檢查是影像圖示,我想在圓形中添加綠色背景色。我在 Swift 中有一個解決方案,但無法在 swiftUI 中實作相同的解決方案。
與我的問題相關的帖子:Add a border with cornerRadius to an Image in SwiftUI Xcode beta 5。但是,這并不能解決我的問題。
此實作的 Swift 代碼:
var imageView = UIImageView()
override init(theme: Theme) {
super.init(theme: theme)
imageView.clipsToBounds = true
setLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
let cornerRadius = frame.height / 2
imageView.setCornerRadius(cornerRadius)
setCornerRadius(cornerRadius)
}

uj5u.com熱心網友回復:
您可以創建此影像,例如...
Image(systemName: "checkmark")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.white)
.padding(20)
.background(Color.green)
.clipShape(Circle())
或者替代...
Image(systemName: "checkmark.circle.fill")
.resizable
.frame(width: 40, height: 40) // put your sizes here
.foregroundColor(.green)
uj5u.com熱心網友回復:
這不是最簡單的事情。將此結構用作單獨的視圖。它將回傳在圓圈上適當大小的影像。
struct ImageOnCircle: View {
let icon: String
let radius: CGFloat
let circleColor: Color
let imageColor: Color // Remove this for an image in your assets folder.
var squareSide: CGFloat {
2.0.squareRoot() * radius
}
var body: some View {
ZStack {
Circle()
.fill(circleColor)
.frame(width: radius * 2, height: radius * 2)
// Use this implementation for an SF Symbol
Image(systemName: icon)
.resizable()
.aspectRatio(1.0, contentMode: .fit)
.frame(width: squareSide, height: squareSide)
.foregroundColor(imageColor)
// Use this implementation for an image in your assets folder.
// Image(icon)
// .resizable()
// .aspectRatio(1.0, contentMode: .fit)
// .frame(width: squareSide, height: squareSide)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389441.html
