7

I did a bit of research, and I see, there is no easy way to make only top right and top left corners of the UIView round, right?

ps. This code makes all 4 corners round which is smth. I don't want.

  #import <QuartzCore/QuartzCore.h>

 self.layer.cornerRadius = 5;
 self.layer.masksToBounds = YES;
lukaswelte
  • 2,951
  • 1
  • 23
  • 45
user1903992
  • 465
  • 2
  • 8
  • 16
  • 1
    http://stackoverflow.com/questions/4847163/round-two-corners-in-uiview?rq=1 – Ramaraj T Dec 28 '12 at 10:14
  • possible duplicate of [how to set cornerRadius for only top-left and top-right corner of a UIView?](http://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview) – Daij-Djan Dec 28 '12 at 13:40

2 Answers2

21

You can do it with this snippet:

// Set top right & left corners
[self setMaskTo:yourView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];

EDIT

Sorry, I forgot this

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(8.0, 8.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];    
    view.layer.mask = shape;
}

PS: as I said in the comment, if you use things like this when displaying UITableViewCell cells, I've found that it's always a better choice to use background images because this kind of drawing stuff always affects performance.

amb
  • 4,798
  • 6
  • 41
  • 68
  • 1
    FYI, if you use things like this when displaying `UITableViewCell` cells, I've found that it's always a better choice to use background images because this kind of drawing stuff always affects performance. – amb Dec 28 '12 at 10:17
  • 2
    this comment should probably be said in the answer, masking a view is a big performance hit and should always be with background images if you can help it – wattson12 Dec 28 '12 at 10:35
  • Thanks for the comment it works! ps. I am not using it as a UITableViewcell, anyway, even if I was I wouldn't know really how to do this using background images... :((( but thanks anyway. – user1903992 Dec 28 '12 at 11:33
  • 1
    @amb +1 Nice answer badge on the way :) – albertamg Jan 22 '13 at 16:46
  • I have tried it but it only round top left and bottom left corner not work for other bottom right and top right. Please suggest. – Chandni Sep 20 '17 at 06:55
0

I tried extensions for the UIView once and it did not work for me because it have caused problems when drawing a cell

I got a better and easier solution now in 2 steps that worked perfectly good :)

1) One line of code (Swift 2.0) - it wounds all 4 corners of a view:

YourView.layer.cornerRadius = 10

2) and one addition in storyboard you add 1 view with regular corners where you need no round corners and adjust constraints as you need : note that small regular corner View should be behind the main view. This is how it looks in storyboard

Here you can see constraints needed for the small view

Gulz
  • 1,773
  • 19
  • 15