簡體   English   中英

如何使子視圖在iOS中成為超級視圖?

[英]How to make sub view to be round corner as super view in iOS?

我使用以下方法將視圖(淺灰色)設置為圓角:

layer.masksToBounds = false
layer.cornerRadius = 10

工作正常。 但是,當添加子視圖(深灰色)時,該子視圖並不像其超級視圖那樣是圓形的。 圓角視圖 如何使子視圖成為超級視圖?

在添加子視圖時,請將masksToBounds設置為true

subView.masksToBounds = true

嘗試也將cornerRadius添加到子視圖。

subview.layer.masksToBounds = false
subview.layer.cornerRadius = 10

更新

通過使用UIBezierPath我們可以將圓度添加到所需的任何角。

subview.roundCorners([.topLeft, .topRight, .bottomRight], radius: 6)

延期

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM