简体   繁体   English

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

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

The width(40) and height(40) of the UIView are equal. UIView的width(40)和height(40)相等。

I have tried these but they didn't work for me: 我尝试了这些,但它们对我没有用:

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

and also 并且

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

Thanks 谢谢

In addition to @Stephan answer. 除了@Stephan答案。

With less code: 用更少的代码:

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

Result: 结果:

IMG

You can use a mask layer like this: 您可以像这样使用遮罩层:

override func viewDidLoad() {
    super.viewDidLoad()

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

Example Output 示例输出

Here the output of a UIView with width/height = 128/128: 这是宽度/高度= 128/128的UIView的输出:

示例输出

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

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