有没有办法将UINavigationController
的导航栏背景设置为纯色?
我知道可以更改Tint颜色,但那样仍然会留下渐变/玻璃效果。
有没有办法消除它,并只使用纯色背景?
有没有办法将UINavigationController
的导航栏背景设置为纯色?
我知道可以更改Tint颜色,但那样仍然会留下渐变/玻璃效果。
有没有办法消除它,并只使用纯色背景?
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
我认为您需要子类化UINavigationBar并覆盖-(void)drawRect:(CGRect)rect
方法:
UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);
[[UINavigationBar appearance] .....];
- MartinHN[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
我曾经重写drawRect方法并填充颜色;但是在iOS7升级后,它会导致UINavigationBar上出现一些问题。如果你编写自己的drawrect方法,即使调用[super drawRect],它也会改变栏的尺寸,最终导致navigationBar高度为44像素。状态栏则为空。
为了获得一个纯色的navigationBar,我使用了一张图片作为背景图像(任何小的纯色图像都可以,因为你要拉伸它),并在UINavigationBar子类的initWithFrame方法中添加了这些行:
[self setBackgroundColor:[UIColor clearColor]]
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0];
}
接着,在你的控制器中使用你的CustomUIViewController。
self.navigationController.navigationBar.backgroundColor
?但这也不起作用 :) - MartinHN