簡體   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