Custom Rounding corners on UIView

后端 未结 1 1976
情深已故
情深已故 2020-11-28 16:43

So I\'ve been stuck on this one for awhile, read and tried many different solutions online, but can\'t figure out what I\'m doing wrong. What I\'m trying to accomplish shou

相关标签:
1条回答
  • 2020-11-28 16:48

    The problem is that vwView gets resized later to fit the screen, but you don't update the path for its new size. There's no particularly trivial fix for this. You basically need to make vwView a custom class (if it's not already) and update the mask in layoutSubviews. Example:

    public class RoundedBorderView: UIView {
    
        public var roundedCorners: UIRectCorner = [ .BottomLeft, .BottomRight ] {
            didSet { self.setNeedsLayout() }
        }
    
        public var cornerRadii: CGSize = CGSizeMake(10, 10) {
            didSet { self.setNeedsLayout() }
        }
    
        public override func layoutSubviews() {
            super.layoutSubviews()
            updateMask()
        }
    
        private func updateMask() {
            let mask = maskShapeLayer()
            mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: roundedCorners, cornerRadii: cornerRadii).CGPath
        }
    
        private func maskShapeLayer() -> CAShapeLayer {
            if let mask = layer.mask as? CAShapeLayer {
                return mask
            }
            let mask = CAShapeLayer()
            layer.mask = mask
            return mask
        }
    
    }
    

    Change the class of vwView to RoundedBorderView and it will maintain its own mask layer.

    0 讨论(0)
提交回复
热议问题