【问题标题】:different custom color for uinavigationbar and itemsuinavigationbar 和项目的不同自定义颜色
【发布时间】:2011-08-01 16:08:50
【问题描述】:

在我的应用程序中,我将覆盖 AppDelegate 中的 uinavigationbar 颜色以在整个应用程序中创建此颜色:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {

UIColor *color = [UIColor colorWithRed:0.16
                                 green:0.20
                                  blue:0.32
                                 alpha:1];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
[self setBarStyle:UIBarStyleBlack];
[self setTintColor:color];


}

但是,在我的一个观点中,我想将其中一个导航栏项目的颜色更改为另一种颜色,与上面的全局颜色不同,但仅限于其中一个项目 - 栏颜色应该保持不变相同(推理 - 我想要一个绿色的导航栏项目和一个“开启”文本,并根据用户输入将其更改为红色并带有一个“关闭”文本)。

我尝试通过以下方式覆盖视图中按钮的颜色,但它似乎没有做任何事情。

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
    [self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
}

有没有人有建议(或更好的方法)来实现这一点?

干杯。

【问题讨论】:

    标签: iphone objective-c uinavigationcontroller uinavigationbar uinavigationitem


    【解决方案1】:

    这是经过测试的代码,可以 100% 工作。

    //在下面的代码中你可以为不同的颜色设置不同的图像

    简单

    用您的问题代码填充颜色。

    下面是把图片放到导航栏

    你可以通过去掉图片代码和把你上面的代码用不同的颜色来定制。逻辑是一样的

    CustomNavigation.h

        #import <Foundation/Foundation.h>
    
    
        @interface UINavigationBar (UINavigationBarCustomDraw){
    
        }
    
        @end
    

    CustomNavigation.m

        @implementation UINavigationBar (UINavigationBarCustomDraw)
    
        - (void) drawRect:(CGRect)rect {
    
            [self setTintColor:[UIColor colorWithRed:0.5f
                                               green: 0.5f
                                                blue:0 
                                               alpha:1]];
    
            if ([self.topItem.title length] > 0) {
    
    
                if ([self.topItem.title isEqualToString:@"First"]) {
    
                    [[UIImage imageNamed:@"First.png"] drawInRect:rect];
    
                }
    
                else if ([self.topItem.title isEqualToString:@"Second"]) {
    
                    [[UIImage imageNamed:@"Second.png"] drawInRect:rect];                   
    
                }
    
    
    
    
                CGRect frame = CGRectMake(0, 0, 320, 44);
                UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
                [label setBackgroundColor:[UIColor clearColor]];
                label.font = [UIFont boldSystemFontOfSize: 20.0];
                label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
                label.textAlignment = UITextAlignmentCenter;
                label.textColor = [UIColor whiteColor];
                label.text = self.topItem.title;
                self.topItem.titleView = label;
    
    
    
    
    
            } 
    
    
            else {
                [[UIImage imageNamed:@"wood.png"] drawInRect:rect];
                self.topItem.titleView = [[[UIView alloc] init] autorelease];
            }
    
    
    
        }
    
        @end
    

    如果你想用 First.png 在 FirstViewController 中设置 navigationBar 背景图片 那么

    在你的 FirstViewController.m 中

            -(void) viewWillAppear:(BOOL)animated{
    
            [super viewWillAppear:animated];
    
    
    
                self.title=@"First";
                [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
    
        }
    

    如果你想通过 Second.png 在 SecondViewController 中设置 navigationBar 背景图片,那么

    在你的 SecondViewController.m 中

            -(void) viewWillAppear:(BOOL)animated{
    
            [super viewWillAppear:animated];
    
    
    
                self.title=@"Second";
                [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
    
        }
    

    【讨论】:

    • 谢谢,Vijay,但这很令人困惑,并没有真正回答我的问题....什么是 CustomNavigation.m?我不想为此创建一个自定义类,我只想用不同的颜色绘制导航栏项目。我也不想用图片。
    • 每个人都应该记住,使用 -drawRect 自定义导航栏将不再适用于即将发布的某个即将发布的 iOS 版本。
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多