简体   繁体   中英

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

The width(40) and height(40) of the UIView are equal.

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.

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:

示例输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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