加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 更改导航项目的背景颜色(条形图)

发布时间:2020-12-14 19:58:02 所属栏目:百科 来源:网络整理
导读:有一种简单的方法可以在视图顶部更改导航项的背景颜色吗?我有一个基于导航的应用程序,我只希望一个视图获得另一种背景颜色.我主要用IB创建了视图.我找到了以下解决方案(未经测试): float r=10;float g=55;float b=130;float a=0;UILabel *label = [[UILabe
有一种简单的方法可以在视图顶部更改导航项的背景颜色吗?我有一个基于导航的应用程序,我只希望一个视图获得另一种背景颜色.我主要用IB创建了视图.我找到了以下解决方案(未经测试):

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(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(但在每个视图中没有相同的颜色)吗?

提前谢谢了!

干杯

解决方法

你可以使用:

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

这会将导航栏的颜色和所有它的按钮着色为特定颜色,在本例中为红色.也可以在Interface Builder中设置此属性.

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

头文件.

#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,image.size.height);
        CGContextScaleCTM(ctx,1.0,-1.0);
        CGContextDrawImage(ctx,CGRectMake(0,self.frame.size.width,self.frame.size.height),image.CGImage);
    }else{
        [super drawLayer:layer inContext:ctx];
    }

}

@end

然后在Interface Builder中将您的UINavigationBar类设置为(在本例中)IdentityLavigationBar下的IdentityNavigationBar.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读