Swift 在 UIVIew 中更改背景颜色和圆角?
Swift change backgroundcolor and have rounded corners in an UIVIew?
我在更改背景颜色和同时制作圆角时遇到问题。
droppedView.roundCorners(corners: .bottomLeft, radius: 7)
droppedView.roundCorners(corners: .bottomRight, radius: 7)
droppedView.backgroundColor = .systemGray6
当我这样做时,我的视图有圆角,但没有背景颜色。
这个问题有解决办法吗?
你可以使用 .clipShape
:
droppedView.background(Color.systemGray6)
droppedView.clipShape(Rectangle().roundCorners(corners: [.bottomLeft, .bottomRight], radius: 7))
UIKit
您可以在 UIKit 中使用 layer.cornerRadius
:
droppedView.clipsToBounds = true
droppedView.layer.cornerRadius = 7
droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
droppedView.backgroundColor = .systemGray6
SwiftUI
clip.Shape
在 SwiftUI 中可用。
首先,创建文件视图+Extensions.swift:
import SwiftUI
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape( RoundedCorner(radius: radius, corners: corners))
}
}
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
}
然后,转到 View.Swift
:
Rectangle().cornerRadius(50, corners: .bottomRight)
Rectangle().cornerRadius(50, corners: .bottomLeft)
试试这个解决方案可能对你有帮助
droppedView.clipsToBounds = true
droppedView.layer.cornerRadius = 7
droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
droppedView.backgroundColor = .systemGray6
我在更改背景颜色和同时制作圆角时遇到问题。
droppedView.roundCorners(corners: .bottomLeft, radius: 7)
droppedView.roundCorners(corners: .bottomRight, radius: 7)
droppedView.backgroundColor = .systemGray6
当我这样做时,我的视图有圆角,但没有背景颜色。
这个问题有解决办法吗?
你可以使用 .clipShape
:
droppedView.background(Color.systemGray6)
droppedView.clipShape(Rectangle().roundCorners(corners: [.bottomLeft, .bottomRight], radius: 7))
UIKit
您可以在 UIKit 中使用 layer.cornerRadius
:
droppedView.clipsToBounds = true
droppedView.layer.cornerRadius = 7
droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
droppedView.backgroundColor = .systemGray6
SwiftUI
clip.Shape
在 SwiftUI 中可用。
首先,创建文件视图+Extensions.swift:
import SwiftUI
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape( RoundedCorner(radius: radius, corners: corners))
}
}
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
}
然后,转到 View.Swift
:
Rectangle().cornerRadius(50, corners: .bottomRight)
Rectangle().cornerRadius(50, corners: .bottomLeft)
试试这个解决方案可能对你有帮助
droppedView.clipsToBounds = true
droppedView.layer.cornerRadius = 7
droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
droppedView.backgroundColor = .systemGray6