Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make specific corners of UIView round

Tags:

ios

iphone

uiview

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;
like image 223
user1903992 Avatar asked Dec 28 '12 10:12

user1903992


People also ask

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.

How do you set corner radius for UIView in storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)

How do you round one corner in Swift?

If you want only some corners to be rounded, you should set the layer's maskedCorners property to be an array of the corners you want – it's an option set, so you can set one or many depending on your needs.


1 Answers

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.

like image 103
amb Avatar answered Oct 22 '22 12:10

amb