UIToolbar with rounded corners
Asked Answered
V

2

7

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!

Volution answered 6/7, 2010 at 3:28 Comment(0)
P
17

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];
}
Pith answered 6/7, 2010 at 4:56 Comment(1)
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...Volution
W
2

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.

Wooten answered 6/7, 2010 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.