5

Possible Duplicate:
UIView with rounded corners

I want to have a view with rounded corners rather then sharp. Is there some default way of doing this?

Community
  • 1
  • 1
Jumhyn
  • 6,687
  • 9
  • 48
  • 76
  • 2
    Dup (with excellent answers): http://stackoverflow.com/questions/1509547/uiview-with-rounded-corners – Kevin L. Dec 16 '10 at 01:45

2 Answers2

14

You can do this by manipulating the layer of the view and its masksToBounds property. I have the following code in a UIView category:

#import <QuartzCore/QuartzCore.h>
...
- (void)addRoundedCornersWithRadius:(NSInteger)cornerRadiusInPixels
{
    self.layer.cornerRadius = cornerRadiusInPixels;
    self.layer.masksToBounds = YES;
    self.opaque = NO;
}

- (void)makeEndsRounded
{
    CGFloat minSide = fmin(self.bounds.size.width, self.bounds.size.height);
    [self addRoundedCornersWithRadius:minSide/2];
}
freespace
  • 16,529
  • 4
  • 36
  • 58
  • What is the purpose of the line "self.layer.masksToBounds = YES;"? – Jumhyn Dec 16 '10 at 02:21
  • 1
    Sublayers are also clipped. To do the above you actually only need to import `CALayer.h`, but there's no gain in importing a file instead of a framework. Rather, header files like `CALayer.h` contain comments that will help and you can look through these if you have a question. – Ken Dec 16 '10 at 02:29
  • I'll also say that in my limited experience animating a rounded layer is expensive for the iPhone. – Ken Dec 16 '10 at 02:34
0

Not that I know of - since UIViews are specified by CGRects they are technically always rectangular. If you want a rounded effect, you need a background image with rounded corners.

etolstoy
  • 1,798
  • 21
  • 33
prgmast3r
  • 413
  • 5
  • 8