简体   繁体   English

更改导航项的背景颜色(条)

[英]change background color of navigation item (bar)

is there an easy way to change the background color of the navigation item on top of a view? 有一种简单的方法可以在视图顶部更改导航项的背景颜色吗? I have a navigation based app and I only want that one view gets another background color. 我有一个基于导航的应用程序,我只希望一个视图获得另一种背景颜色。 I created the views mainly with IB. 我主要用IB创建了视图。 I found the following solution (not tested): 我找到了以下解决方案(未经测试):

float r=10;
float g=55;
float b=130;
float a=0;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setBackgroundColor:[UIColor colorWithRed:r/255.f
                                          green:g/255.f
                                           blue:b/255.f    
                                          alpha:a/255.f];];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];

Is there an easier way like 是否有更简单的方式

[navigationController.navigationBar setBackgroundColor:blueColor]

Or can I do it with IB (but without having the same color in every view)? 或者我可以使用IB(但在每个视图中没有相同的颜色)吗?

Many thanks in advance! 提前谢谢了!

Cheers 干杯

You could use: 你可以使用:

[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example.

This would tint the color of the Navigation Bar and all it's Buttons to a specific color, in this case red. 这会将导航栏的颜色和所有它的按钮着色为特定颜色,在本例中为红色。 This property can also be set in Interface Builder. 也可以在Interface Builder中设置此属性。

And if you wanted to customize it further, you can set the background of the UINavigationBar to an image by sub-classing it. 如果您想进一步自定义它,可以通过对其进行子类化将UINavigationBar的背景设置为图像。 Like so… 像这样......

Header File. 头文件。

#import <UIKit/UIKit.h>

@interface UINavigationBar (CustomImage)

@end

Implementaion File. 实现文件。

#import "CustomNavigationBar.h"

@implementation UINavigationBar (CustomImage)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{
    if([self isMemberOfClass: [UINavigationBar class]]){
        UIImage *image = [UIImage imageNamed:@"bar.png"];
        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
    }else{
        [super drawLayer:layer inContext:ctx];
    }

}

@end

Then in Interface Builder set the class of you UINavigationBar to (in this case) CustomNavigationBar under the Identity Tab. 然后在界面生成器设置你的类UINavigationBar (在这种情况下) CustomNavigationBar下的身份标签。

Try this in Interface Builder on your UINavigationController . UINavigationController上的Interface Builder中尝试这个。

在此输入图像描述

[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]]; [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0 / 256.0 green:145.0 / 256.0 blue:35.0 / 256.0 alpha:1.0]]; Will change the color of the navigation bar of whole app. 将改变整个应用程序的导航栏的颜色。

Just place it in Appdelegate's didFinishLauncing method. 只需将其放在Appdelegate的didFinishLauncing方法中即可。

@jerry @杰瑞

tint color is not supported to the RGB Coordinates RGB坐标不支持色调颜色

你可以使用navigationController.navigationBar.tintColor;

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

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