我正在尝试使用UIBezierPath设置UIView的角半径。 我为此创建了以下扩展名:
extension UIView
{
func roundedView(usingCorners corners: UIRectCorner, cornerRadii: CGSize)
{
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: cornerRadii)
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}
cvTutorialContainerView.roundedView(usingCorners: [.topRight, .bottomRight, .topLeft, .bottomLeft],
cornerRadii: CGSize(width: 8, height: 8))
正在从viewDidLayoutSubviews()
调用该函数:
cvTutorialContainerView.roundedView(usingCorners: [.topRight, .bottomRight, .topLeft, .bottomLeft],
cornerRadii: CGSize(width: 8, height: 8))
问题是,由于某些原因,但仅在某些情况下(例如,使用UIPageView或UIScrollView时),我仅获得了顶角的半径。
我尝试了cvTutorialContainerView.layer.cornerRadius = 8.0
,但没有成功。我也尝试过从viewDidLoad和viewDidAppear调用该函数,但是再次失败了。
有什么想法吗?
答案 0 :(得分:0)
为什么要绘制bezierPath并应用遮罩?
yourView.layer.masksToBounds = true // You seem to be missing this part
如果未指定上述属性,则您的subviews
不会被裁剪到yourView
的图层上,这可能就是以下行最初对您不起作用的原因
yourView.layer.cornerRadius = 8 //your radius
P.S。您还可以使用以下方法对特定角进行四舍五入:
yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] //This will round the top-left and top-right corners