Ios 仅视图一侧的圆角

Ios 仅视图一侧的圆角,ios,swift,uiview,Ios,Swift,Uiview,我知道如何通过如下设置角半径使所有四个角圆: func rounded() { self.layer.cornerRadius = self.frame.size.height / 2 } 但我不知道如何只拐弯两个角。感谢您的帮助 我尝试只绕过UIView的一个边角,如下所示:您可以尝试 extension UIView { func roundedLeftTopBottom(){ self.clipsToBounds = true

我知道如何通过如下设置角半径使所有四个角圆:

 func rounded() {
        self.layer.cornerRadius = self.frame.size.height / 2
    }
但我不知道如何只拐弯两个角。感谢您的帮助

我尝试只绕过UIView的一个边角,如下所示:

您可以尝试

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中,请确保调用LayoutSubView或ViewDidLayoutSubView中的任何一个视图,以便仅在iOS11上具有正确的帧大小,确实如此op可以根据具体情况自由使用任何人我尝试使用UIView扩展,但似乎在应该进行舍入的地方有空白。@Shu Khan我刚刚添加了我所说内容的屏幕截图你可以设置view.clipsToBounds=true