繁体   English   中英

将NavigationBar背景设置为纯色

[英]Set NavigationBar background to a solid color

有什么办法可以将UINavigationController导航栏的背景设置为纯色吗?

我知道我可以改变Tint颜色,但这仍然留给我渐变/玻璃效果。

我可以用任何方式摆脱它,只是有一个简单的旧纯色?

以下代码还会导致导航栏的纯色:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];

我认为你必须-(void)drawRect:(CGRect)rect UINavigationBar并覆盖-(void)drawRect:(CGRect)rect

UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);

我曾经覆盖drawRect方法并填充颜色; 但是在iOS7升级之后,它会导致UINavigationBar出现一些问题。 如果您编写自己的drawrect方法,即使您调用[super drawRect],它也会更改条​​形的尺寸,最终会得到一个高度为44像素的navigationBar。 状态栏保留为空。

为了得到一个纯色的navigationBar,我使用了一个图像作为背景图像(任何小的纯色图像都可以自己拉伸它)并在UINavigationBar子类的initWithFrame方法中添加这些行:

[self setBackgroundColor:[UIColor clearColor]]     
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 

以下代码对我有用:

[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]];

创建一个CustomUIViewController ,扩展UIViewController并覆盖viewDidLoad ,如下所示:

- (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。

积分

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM