开发者

UIToolbar with rounded corners

I want to display a UIToolbar with rounded corners on top, what would be the easiest way? th开发者_开发百科e 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜