简体   繁体   中英

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!

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];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM