Jackelope11
Jackelope11

Reputation: 1171

Rounded Rectangle Issue

I have seen many Q's on this subject but none are exactly what I am trying to do. I have a view inside of a view and it's frame is a CGRectangle. I would like said rectangle to have rounded edges to look like a rounded rectangle button. If I could have some code examples on how to implement that it would be nice. Thank you in advance.

Upvotes: 0

Views: 370

Answers (2)

dbslone
dbslone

Reputation: 2034

You need to make sure you import <QuartzCore/QuartzCore.h> and add QuartzCore to the Existing Frameworks in order to gain access to the cornerRadius: method.

Then to set the corner radius you would use something like the following depending upon your implementation of the view

UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,200)];
CALayer *theViewLayer = [theView layer];
[theViewLayer setCorderRadius:5.0];

//Other Methods you can use
[theViewLayer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:0.3] CGColor]];
[theViewLayer setBorderWidth:2.0];
[theViewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];

Upvotes: 2

Francesco Puglisi
Francesco Puglisi

Reputation: 2177

You first need to import QuartzCore framework into your project. I hope you know how to do that. Than you can use the following code:

CALayer *l = [yourView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:1.0];
[l setBorderColor:[[UIColor blackColor] CGColor]];

Hope it helps! ;)

Upvotes: 5

Related Questions