How to set cornerRadius for only top-left and top-right corner of a UIView?

后端 未结 26 2934
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:14

Is there a way to set cornerRadius for only top-left and top-right corner of a UIView?

I tried the following, but it end up not seeing the

相关标签:
26条回答
  • 2020-11-22 07:01

    Swift code example here: https://stackoverflow.com/a/35621736/308315


    Not directly. You will have to:

    1. Create a CAShapeLayer
    2. Set its path to be a CGPathRef based on view.bounds but with only two rounded corners (probably by using +[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:])
    3. Set your view.layer.mask to be the CAShapeLayer
    0 讨论(0)
  • 2020-11-22 07:03

    A way to do this programmatically would be to create a UIView over the top part of the UIView that has the rounded corners. Or you could hide the top underneath something.

    0 讨论(0)
  • 2020-11-22 07:04

    The easiest way would be to make a mask with a rounded corner layer.

    CALayer *maskLayer = [CALayer layer];
    maskLayer.frame = CGRectMake(0,0,maskWidth ,maskHeight);
    maskLayer.contents = (__bridge id)[[UIImage imageNamed:@"maskImageWithRoundedCorners.png"] CGImage];
    
    aUIView.layer.mask = maskLayer;
    

    And don't forget to:

    #import <QuartzCore/QuartzCore.h>
    
    0 讨论(0)
  • 2020-11-22 07:05

    Here is a Swift version of @JohnnyRockex answer

    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
        }
    
    }
    

    view.roundCorners([.topLeft, .bottomRight], radius: 10)
    

    Note

    If you're using Auto Layout, you'll need to subclass your UIView and call roundCorners in the view's layoutSubviews for optimal effect.

    class View: UIView {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            self.roundCorners([.topLeft, .bottomLeft], radius: 10)
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:05

    Try this code,

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:( UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path  = maskPath.CGPath;
    
    view.layer.mask = maskLayer;
    
    0 讨论(0)
  • 2020-11-22 07:05
        // Create the path (with only the top-left corner rounded)
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds 
                               byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) 
                               cornerRadii:CGSizeMake(7.0, 7.0)];
    
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.stripBlackImnageView.bounds;
    maskLayer.path = maskPath.CGPath; 
    // Set the newly created shapelayer as the mask for the image view's layer
    view.layer.mask = maskLayer;
    
    0 讨论(0)
提交回复
热议问题