我知道自定义 UINavigationBar 有两种不同的方法:您可以将背景图像设置为一些自定义 png,或者您可以通过编程方式在代码中自定义栏的外观。如果我的目标是支持 iOS 4、5、6,尝试通过代码自定义栏的外观是否有意义?
我本质上想要一个没有任何渐变、纯色、圆角和极细阴影线的导航栏。我会在下面发布一张图片,但我至少需要 10 个声望点 :(
我开始使用以下代码来解决无渐变和纯色问题,并将其放在我的 rootviewcontroller 的 .m 文件中,然后放在类本身的 @implementation 之前,但无济于事:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blueColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}
@end
我还实现了这段代码来圆角,这很有效:
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;
对此事的任何帮助将不胜感激!