uiview的仅一侧的圆角

时间:2019-03-17 17:36:38

标签: ios swift uiview

Rounded corner

我知道如何通过设置类似于以下内容的拐角半径来对所有四个角进行圆角处理:

 func rounded() {
        self.layer.cornerRadius = self.frame.size.height / 2
    }

但不确定如何仅将2个圆角倒圆。任何帮助表示赞赏。

enter image description here我正试图像这样将UIView的仅一个角弄圆:

1 个答案:

答案 0 :(得分:0)

您可以尝试

extension UIView {
    func roundedLeftTopBottom(){
        self.clipsToBounds = true
        let maskPath1 = UIBezierPath(roundedRect: bounds,
                                     byRoundingCorners: [.topLeft , .bottomLeft],
                                     cornerRadii: CGSize(width:self.frame.size.height / 2, height:self.frame.size.height / 2))
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = bounds
        maskLayer1.path = maskPath1.cgPath
        layer.mask = maskLayer1
    }

    // or

    func round () {
        self.layer.cornerRadius = self.frame.size.height / 2
        self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
    }
}

如果视图位于vc内,请确保在layoutSubviewsviewDidLayoutSubviews内调用其中任何一个,以使帧尺寸正确