Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a rounded-corner view

Good day!

Im trying to make my view (view in main view) make rounded corner. Im doing like this, but it doesn't work. Any ideas?

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
    currenView = [[UIView alloc] init]; 

    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:currenView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(30.0, 30.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = currenView.bounds;
    maskLayer.path = maskPath.CGPath;
    currenView.layer.mask = maskLayer;


}
return self;
like image 549
Pavel Avatar asked Feb 01 '13 13:02

Pavel


People also ask

How do you round the corners of a view?

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. It is just that the first is used with UIView and the second is used with CALayer .

How can you created rounded corners?

To create a rounded corner, we use the CSS border-radius property. This property is used to set the border-radius of element.

Can you do rounded corners in Powerpoint?

Using the Insert tab on the main menu bar, click on Shapes and then Rectangles, and choose Rectangle: rounded corners.


2 Answers

Try something like this:

view.layer.cornerRadius = 5.0;
view.layer.masksToBounds = YES;

for shadow:

view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
view.layer.masksToBounds = NO;
view.layer.shadowRadius = 5.0f;

Make sure to import <QuartzCore/QuartzCore.h>

like image 122
Stavash Avatar answered Sep 22 '22 23:09

Stavash


Here is the code. Alloc init a view and send to this method to get corners rounded. You can optionally round any of the corners u want. Also give shadow stroke color.

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

CAShapeLayer* shape = [[[CAShapeLayer alloc] init] autorelease];
[shape setPath:rounded.CGPath];
shape.strokeColor = [[UIColor grayColor] CGColor];

view.backgroundColor=color;
view.layer.mask = shape;
}

Call the method like this.

[self setMaskTo:ABCView byRoundingCorners:UIRectCornerAllCorners withColor:[UIColor greenColor]];
like image 42
NaXir Avatar answered Sep 22 '22 23:09

NaXir