objective c - 带圆角的UINavigationBar

时间:2012-04-20 16:16:00

标签: objective-c uinavigationcontroller uinavigationbar cglayer

我想让我的导航栏成为一个圆角(左上角和右上角)

我发现这个代码可以实现:

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;

但它有一个问题,现在nab条没有角边,它在角落里是透明的,我可以看到它后面的视图。
我不希望它透明,我想在那里放一些黑色。
有谁知道怎么做?

1 个答案:

答案 0 :(得分:5)

假设这是一个顶级导航控制器,这样的东西应该适合你:

self.window.backgroundColor = [UIColor blackColor];

坚持在application:didFinishLaunchingWithOptions:

如果不这样做,只需使用带黑角的导航栏图像:

enter image description here