[英]Set NavigationBar background to a solid color
Is there any way I can set the background of the Navigation Bar of the UINavigationController
to a solid color? 有什么办法可以将
UINavigationController
导航栏的背景设置为纯色吗?
I know I can change the Tint color, but that still leaves me with the gradient/glass effect. 我知道我可以改变Tint颜色,但这仍然留给我渐变/玻璃效果。
Any way I can get rid of that, and just have a plain old solid color? 我可以用任何方式摆脱它,只是有一个简单的旧纯色?
The following code also results in solid color of navigation bar: 以下代码还会导致导航栏的纯色:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
I think you have to subclass UINavigationBar and override -(void)drawRect:(CGRect)rect
: 我认为你必须
-(void)drawRect:(CGRect)rect
UINavigationBar并覆盖-(void)drawRect:(CGRect)rect
:
UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);
I used to override drawRect method and fill color; 我曾经覆盖drawRect方法并填充颜色; but after iOS7 upgrade it causes some problems on UINavigationBar.
但是在iOS7升级之后,它会导致UINavigationBar出现一些问题。 If you write your own drawrect method, even if you call [super drawRect], it changes the bar's dimension and you end up with a navigationBar with 44 pixels height.
如果您编写自己的drawrect方法,即使您调用[super drawRect],它也会更改条形的尺寸,最终会得到一个高度为44像素的navigationBar。 The status bar is left empty.
状态栏保留为空。
To get a solid colored navigationBar, I used an image as background image (any small solid colored image will do since you are stretching it) and added this lines inside the initWithFrame method of the UINavigationBar subclass: 为了得到一个纯色的navigationBar,我使用了一个图像作为背景图像(任何小的纯色图像都可以自己拉伸它)并在UINavigationBar子类的initWithFrame方法中添加这些行:
[self setBackgroundColor:[UIColor clearColor]]
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
The following code worked for me: 以下代码对我有用:
[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]];
Create a CustomUIViewController that extends UIViewController and override viewDidLoad to something like this: 创建一个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];
}
After that, use your CustomUIViewController in your controllers. 之后,在控制器中使用CustomUIViewController。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.