UINavigationBar Rounded Two Corners

2019-01-30 09:31发布

I want to round the top right and top left of the UINavigationBar.

I know that there are functions for changing the views corner radius, but is it possible to do something similar to the standard UINavigationBar?

If you don't know what I'm talking about, check out this:

Thanks!

3条回答
欢心
2楼-- · 2019-01-30 09:46

the following code works for me (tested on iOS5). The following code rounds the top left/right corners of the main navigation bar. Additionally, it adds a shadow to it:

CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];  
[capa setShouldRasterize:YES];


//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f;    //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds 
                                               byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;

[capa addSublayer:maskLayer];
capa.mask = maskLayer;
查看更多
Summer. ? 凉城
3楼-- · 2019-01-30 09:48

You might find this helpful. Maybe you could also do some kind of masking with CoreAnim/Image? I'm not very knowledgeable about the Core* family...

http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html

查看更多
劫难
4楼-- · 2019-01-30 09:53

The best way is to override drawRect in UINavigationBar and use a custom image.

查看更多
登录 后发表回答