Round top corners of a UIView and add border

前端 未结 4 504
自闭症患者
自闭症患者 2020-12-03 01:55

I have the following code in a category that does rounding of corners. I would also like to draw a border. But the border is not shown on the rounded part of the corner.

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 02:34

    Swift version of David Berry's answer:

    func roundCorners(corners:UIRectCorner, radius:CGFloat) {
        let bounds = self.bounds
    
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
    
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.CGPath
    
        self.layer.mask = maskLayer
    
        let frameLayer = CAShapeLayer()
        frameLayer.frame = bounds
        frameLayer.path = maskPath.CGPath
        frameLayer.strokeColor = UIColor.redColor().CGColor
        frameLayer.fillColor = nil
    
        self.layer.addSublayer(frameLayer)
    }
    
    func roundTopCornersRadius(radius:CGFloat) {
        self.roundCorners([UIRectCorner.TopLeft, UIRectCorner.TopRight], radius:radius)
    }
    
    func roundBottomCornersRadius(radius:CGFloat) {
        self.roundCorners([UIRectCorner.BottomLeft, UIRectCorner.BottomRight], radius:radius)
    }
    

提交回复
热议问题