Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIToolbar with rounded corners

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!

like image 707
rjobidon Avatar asked Jul 06 '10 03:07

rjobidon


2 Answers

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];
}
like image 114
Sagar Kothari Avatar answered Sep 22 '22 08:09

Sagar Kothari


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.

like image 34
drawnonward Avatar answered Sep 19 '22 08:09

drawnonward