0

I'm trying to get a UIImageView to have its top corners rounded - and the top corners only.

Here's my code, that I based off the higher-voted answer in this question:

CouponViewController.m:

@synthesize cv;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self loadXibWithName:@"CouponView"];
    cv = (CouponView *)self.view;
    cv.delegate = self;

    [self.navigationController.navigationBar setHidden:NO];
    //[self displayLogoInNavBar];

    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:cv.couponImage.bounds
                                     byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                           cornerRadii:CGSizeMake(15.0, 15.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = cv.bounds;
    maskLayer.path = maskPath.CGPath;
    cv.couponImage.layer.mask = maskLayer;
}

The only thing different there (besides the variables) is the absence of [maskLayer release], which caused an ARC error, and doesn't seem to help when said error is fixed, anyway.

There's also a CouponView.m, but that contains the IBActions and an awakeFromNib, that doesn't seem to do anything - I tried putting the following test code there:

- (void)awakeFromNib {
    self.couponImage.layer.cornerRadius = 15.0f;
    self.couponPerforated.image = [UIImage imageNamed:@"coupon_perforation_191"];
}

And finally, here's the CouponView.xib itself:

enter image description here

When it loads, the image is still a square.

Am I missing anything?

Community
  • 1
  • 1
zack_falcon
  • 4,186
  • 20
  • 62
  • 108
  • 2
    Try change this line: maskLayer.frame = cv.bounds; to maskLayer.frame = cv.couponImage.bounds; – Greg Jul 20 '15 at 16:14
  • @Greg That worked, albeit it somehow resized my image to half, in `viewDidLoad`. Putting it in `viewDidAppear` did the trick, although the image visibly changes from a square to rounded top edges. – zack_falcon Jul 21 '15 at 22:43

1 Answers1

1

Try this code. After long time search it helps me.

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self setMaskTo:viewDistance byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
}

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(20.0, 20.0)];

    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    shape.frame = self.view.bounds;
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}
Chirag Patel
  • 1,453
  • 1
  • 11
  • 21