更改导航项目的背景颜色(条形) [英] change background color of navigation item (bar)

查看:136
本文介绍了更改导航项目的背景颜色(条形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种简单的方法可以在视图顶部更改导航项的背景颜色吗?我有一个基于导航的应用程序,我只希望一个视图获得另一种背景颜色。我主要用IB创建了视图。我找到了以下解决方案(未经测试):

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

是否有更轻松的方式

[navigationController.navigationBar setBackgroundColor:blueColor]

或者我可以做它与IB(但在每个视图中没有相同的颜色)?

Or can I do it with IB (but without having the same color in every view)?

非常感谢提前!

干杯

推荐答案

您可以使用:

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

这会将导航栏的颜色和所有按钮的颜色设置为特定颜色,在这种情况下红色。此属性也可以在Interface Builder中设置。

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.

如果您想进一步自定义它,可以设置 UINavigationBar的背景通过对图像进行子类化来对图像进行分类。像这样...

And if you wanted to customize it further, you can set the background of the UINavigationBar to an image by sub-classing it. Like so…

标题文件。

#import <UIKit/UIKit.h>

@interface UINavigationBar (CustomImage)

@end

实施文件。

#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

然后在Interface Builder中,在Identity选项卡下设置 UINavigationBar 的类(在本例中) CustomNavigationBar

Then in Interface Builder set the class of you UINavigationBar to (in this case) CustomNavigationBar under the Identity Tab.

这篇关于更改导航项目的背景颜色(条形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆