uinavigationbar 和项目的不同自定义颜色

发布于 2024-11-27 03:19:59 字数 986 浏览 1 评论 0原文

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

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

干杯。

In my app I am overriding the uinavigationbar color in the AppDelegate to create this color across the entire app:

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


}

However, in one of my views, i would like to change the color of one of the nav bar items to another color, different from the global color above, but only for one of the items - the bar color should stay the same (reasoning - i'd like to have a nav bar item in green and a "On" text and change it to red with an "Off" text based on the user input).

I tried to override the color of the button in my view in the following way, but it doesnt seem to do anything.

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

Does anyone have a suggestion (or a better way) to make this happen?

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2024-12-04 03:19:59

它是经过测试的代码,可以 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 然后

在你的 FirstViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"First";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }

如果你想 Second.png 在 SecondViewController 中设置导航栏背景图像然后

在你的 SecondViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"Second";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }

It is tested code which will work 100%.

//in below code u can set the different image for different color

or

simply

fill the color by your question code.

here below is for put image in navigation bar

u can customize by removeing the picture code and puttting ur above code with different color.that it .logic is same

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

if u want to First.png to set navigationBar background image in FirstViewController then

in ur FirstViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"First";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }

if u want to Second.png to set navigationBar background image in SecondViewController then

in ur SecondViewController.m

        -(void) viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];



            self.title=@"Second";
            [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文