views:

85

answers:

2

Hi! I want to display a UIToolbar with rounded corners on top, what would be the easiest way? the toolbar is not top-aligned on the window; it has a margin all around. Thanks!

A: 

The easiest way to round the corners of a view is with the cornerRadius (and masksToBounds) property of CALayer. However, with that you only have the option of rounding all the corners equally. To use that property, you could put the UIToolbar into another view that was taller than the toolbar, so only the top was rounded. This would work well if another view will have rounded bottom corners.

The easiest way to mask a view to an arbitrary shape is to set the mask property of CALayer to a new CAShapeLayer. In your case, build a CGPath for the CAShapeLayer using CGPathAddLineToPoint and CGPathAddArcToPoint or similar to get only the top corners rounded.

drawnonward
+1  A: 

Very simple.

First of all - have an IBOutlet variable of UIToolbar in your .h file of your view controller. For example.

@interface TextFormattedViewController : UIViewController {
     IBOutlet UIToolbar *tBar;
}

Now in your .m file of your view controller file just place following code & it will work as a magic for you. However- please add comment if any queries.

#import "TextFormattedViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation TextFormattedViewController
- (void)viewDidLoad {
    // following statement is must.
    tBar.clipsToBounds=YES;
    CALayer *l=tBar.layer;
    // set corner radious
    [l setCornerRadius:10];
    // to apply border on corners
    [l setBorderColor:[[UIColor redColor] CGColor]];
    // to apply set border width.
    [l setBorderWidth:5.0];
}
sugar
Thanks sugar for your great suggestion! For best look we can only change setCornerRadius (and drop the two last calls). It produces nice antialiased round corners. However all four corners are rounded...
rjobidon