Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round two corners in UIView

A little while ago I posted a question about rounding just two corners of a view, and got a great response, but am having problems implementing it. Here is my drawRect: method:

- (void)drawRect:(CGRect)rect {     //[super drawRect:rect]; <------Should I uncomment this?     int radius = 5;     CGContextRef context = UIGraphicsGetCurrentContext();     CGContextBeginPath(context);     CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);     CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);     CGContextClosePath(context);     CGContextClip(context); } 

The method is being called, but doesn't seem to affect the outcome of the view. Any ideas why?

like image 222
Jumhyn Avatar asked Jan 31 '11 02:01

Jumhyn


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 add corner radius to a 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)


1 Answers

CACornerMask introduced in iOS 11, which help to define topleft, topright, bottomleft, bottom right in view layer. Below is example to use.

Here I try to rounded only two top corner:

myView.clipsToBounds = true myView.layer.cornerRadius = 10 myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner] 

FYI Ref:

like image 147
SachinVsSachin Avatar answered Oct 08 '22 16:10

SachinVsSachin