How to make a round shape of any kind

I just want to change the shape of the view from square to round. As I try with cornerRadious, but it's just around the corner. Because I want the whole view to be round.

+2
user-interface ios iphone uiview
May 10 '13 at 5:25
source share
3 answers

UIView is always rectangular. However, you can make him look around (or any shape in general) with a mask. To do this, make a UIImage, which is a black circle (on a transparent background). Now take the CGImage of this UIImage and make it the contents of CALayer. Finally, set CALayer as the mask layer of your view.

Suppose your view is 100 x 100. Then (not tested, but should be very correct):

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0); CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor); CGContextFillEllipseInRect(c, CGRectMake(0,0,100,100)); UIImage* maskim = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CALayer* mask = [CALayer new]; mask.frame = CGRectMake(0,0,100,100); mask.contents = (id)maskim.CGImage; view.layer.mask = mask; 
+5
May 10 '13 at 5:35
source share

You can make a rounded border with the border width of any control in this way: -

 CALayer * l1 = [viewPopup layer]; [l1 setMasksToBounds:YES]; [l1 setCornerRadius:5.0]; // You can even add a border [l1 setBorderWidth:5.0]; [l1 setBorderColor:[[UIColor darkGrayColor] CGColor]]; 


Just replace viewPopup with your control.

Note: - Remember to import <QuartzCore/QuartzCore.h>

+2
May 10 '13 at 8:22
source share

Try this code: -

 [roundView.layer setCornerRadius:50.0f]; [roundView.layer setBorderColor:[UIColor lightGrayColor].CGColor]; [roundView.layer setBorderWidth:1.5f]; [roundView.layer setShadowColor:[UIColor blackColor].CGColor]; [roundView.layer setShadowOpacity:0.8]; [roundView.layer setShadowRadius:3.0]; [roundView.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; 

Note. - roundView is your opinion that you want to round.

Hope this helps you. Thanks

+1
May 10 '13 at 5:38
source share



All Articles