6

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;
Stavash
  • 14,244
  • 5
  • 52
  • 80
Pavel
  • 167
  • 2
  • 11

4 Answers4

13

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>

Stavash
  • 14,244
  • 5
  • 52
  • 80
  • I've noticed you're not doing anything with the UIView you're allocating - currenView isn't going anywhere – Stavash Feb 01 '13 at 13:50
  • After this line add view.clipsToBounds = YES – CRDave Feb 01 '13 at 13:50
  • If this is a UIViewController, you need to assign it the view you're creating – Stavash Feb 01 '13 at 13:50
  • @property (nonatomic, strong) IBOutlet UIView *currenView; this view is IBOutlet. – Pavel Feb 01 '13 at 13:53
  • 2
    So why are you instantiating a new one? – Stavash Feb 01 '13 at 13:53
  • 2
    If the view is in IBOutlet then why are using "alloc , init"? – Exploring Feb 01 '13 at 13:54
  • What's happening is this: You're assigning your controller an unrelated UIView which gets run over when viewDidLoad is called, that's when the storyboard/xib view is loaded. Chances are you're not even seeing the view you're creating in your init method. – Stavash Feb 01 '13 at 13:57
  • ok, i got it, thank you. but even than it doesn't work ( 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; currenView.layer.cornerRadius = 5.0; currenView.layer.masksToBounds = YES; – Pavel Feb 01 '13 at 13:59
  • there is one more question: how to make shadow to this view? – Pavel Feb 01 '13 at 14:02
  • Try the edited code. Keep in mind that you can't mask to bounds now. – Stavash Feb 01 '13 at 14:04
  • i did it like in e.g. but the is no shadow – Pavel Feb 01 '13 at 14:27
  • currenView.layer.cornerRadius = 10.0; currenView.layer.masksToBounds = YES; currenView.layer.shadowColor = [UIColor whiteColor].CGColor; currenView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); currenView.layer.masksToBounds = NO; currenView.layer.shadowRadius = 5.0f; – Pavel Feb 01 '13 at 15:39
  • Remove the masksToBounds=YES line just to be sure, also make sure your view isn't set to clipsToBounds=YES – Stavash Feb 01 '13 at 15:41
  • currenView.clipsToBounds = NO; currenView.layer.cornerRadius = 10.0; // currenView.layer.masksToBounds = YES; currenView.layer.shadowColor = [UIColor blackColor].CGColor; currenView.layer.shadowOffset = CGSizeMake(10.0f, 10.0f); // currenView.layer.masksToBounds = NO; currenView.layer.shadowRadius = 5.0f; still there is no shadow yet. – Pavel Feb 04 '13 at 08:44
2

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]];
NaXir
  • 2,373
  • 2
  • 24
  • 31
0

Stavash's solution seems to be correct, I have used it several times. If you are looking for an alternative or insist on using masklayers, see this answer: https://stackoverflow.com/a/13163693/936957

Community
  • 1
  • 1
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
0

Your view has no size. its w and h is 0. Try something like,

currentView = [[UIView alloc] initWithFrame:CGRectMake(0,0 200,200)]; 

and then apply

currentView.layer.cornerRadius = 8.0;
currentView.layer.masksToBounds = YES;
currentView.layer.borderWidth = 1.0; 
karim
  • 15,408
  • 7
  • 58
  • 96
  • view.layer.cornerRadius = 5.0; view.layer.masksToBounds = YES; view.layer.shadowColor = [UIColor blackColor].CGColor; view.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); view.layer.masksToBounds = NO; view.layer.shadowRadius = 5.0f; if i do like this corners are rounded but there is no shadow. i think the problem is in view.layer.masksToBounds = NO; – Pavel Feb 01 '13 at 14:15
  • for shadow follow stavash's solution – karim Feb 01 '13 at 14:17