繁体   English   中英

如何在Swift 4.2中将UIView制成圆形?

[英]How to make an UIView into circle shape in swift 4.2?

UIView的width(40)和height(40)相等。

我尝试了这些,但它们对我没有用:

countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2

并且

self.countView.layer.cornerRadius = (countView.frame.size.width)/2

谢谢

除了@Stephan答案。

用更少的代码:

self.countView.layer.cornerRadius = self.countView.bounds.height / 2

结果:

IMG

您可以像这样使用遮罩层:

override func viewDidLoad() {
    super.viewDidLoad()

    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
    self.countView.layer.mask = maskLayer
}

示例输出

这是宽度/高度= 128/128的UIView的输出:

示例输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM