[英]SWIFT - Setting layer.cornerRadius greater than the view height causes issues
I actually want to add a corner radius to a view on only one side.我实际上只想在一侧的视图中添加角半径。 The radius is supposed to be of full height.
半径应该是全高。 This is my implementation.
这是我的实现。
someView.layer.cornerRadius = someView.frame.size.height
someView.layer.maskedCorners = [.layerMaxXMaxYCorner]
This does the job as required, but it adds extra shapes on other sides as shown below.这会按要求完成工作,但它会在其他侧面添加额外的形状,如下所示。
This looks like an iOS bug, I was able to reproduce it这看起来像一个 iOS 错误,我能够重现它
But usually you don't wanna set cornerRadius
greater than half view minimum side, in your case:但通常你不想设置
cornerRadius
大于半视图最小边,在你的情况下:
someView.layer.cornerRadius = someView.frame.size.height / 2
I assume it'll produce result you're expecting:我假设它会产生您期望的结果:
Corner radius is radius of a circle inscribed in the corner of a rectangle, I think that's why there may be problems with radius bigger than side/2: circle doesn't fit a rectangle anymore角半径是刻在矩形角上的圆的半径,我认为这就是为什么半径大于 side/2 可能会出现问题:圆不再适合矩形
i had the same problem.我有同样的问题。 I needed to make the height of the view 16 and the bottom corners radius also 16.
我需要使视图的高度为 16,底角半径也为 16。
My solution:我的解决方案:
roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
)roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
) code代码
private let bottomView: UIView = {
let view = UIView(forAutoLayout: ())
view.clipsToBounds = true
return view
}()
private let roundedView: UIView = {
let view = UIView(forAutoLayout: ())
view.clipsToBounds = true
view.contentMode = .scaleAspectFill
view.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
return view
}()
...
contentView.addSubview(bottomView)
bottomView.addSubview(roundedView)
...
func setupConstraints() {
if roundedView.layer.cornerRadius >= roundedView.frame.height / 2 {
roundedView.autoSetDimension(.height, toSize: roundedView.layer.cornerRadius * 2)
} else {
roundedView.autoPinEdge(toSuperviewEdge: .top)
}
roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
bottomView.autoPinEdgesToSuperviewEdges()
}
result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.