首页 文章

uinavigationbar和items的不同自定义颜色

提问于
浏览
1

在我的应用程序中,我将覆盖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]];
}

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

干杯 .

1 回答

  • 0

    它是经过测试的代码,可以100%运行 .

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

    要么

    只是

    用问题代码填充颜色 .

    以下是放置导航栏中的图像

    你可以通过删除图片代码并在不同颜色的代码上面进行自定义 . 它.logic是相同的

    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)];
    
        }
    

相关问题