iPhone iOS如何按需重绘UINavigationBar? [英] iPhone iOS how to redraw UINavigationBar on demand?

查看:93
本文介绍了iPhone iOS如何按需重绘UINavigationBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用外观属性更改导航栏颜色的代码。我想立即看到导航栏颜色的变化。但是,我找不到正确的调用来重绘导航栏。当我推动模态导航控制器时,它的颜色会发生变化,从而覆盖了导航栏。

I have code that changes the color of navigation bar using appearance attribute. I would like the change to the navbar color to be visible immediately. However, I cannot find a proper call to make to redraw the navbar. Currently it's color changes when I push a modal navigation controller, thus covering the navbar.

这不起作用:

AppDelegate* appDelegate = ((AppDelegate*)[[UIApplication sharedApplication] delegate]);

    [appDelegate.window setNeedsDisplay];

我也试过要求导航控制器的各种实例重绘他们的视图,但是没有用。

I also tried asking various instances of navigation controller to redraw their views, which did not work.

要求UINavigationBar根据需要重绘自己的正确方法是什么?

谢谢你好!

更新:这是有效的代码!

    - (UIColor *) colorOfPoint:(CGPoint)point
    {
        unsigned char pixel[4] = {0};
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

        CGContextTranslateCTM(context, -point.x, -point.y);

        [self.view.layer renderInContext:context];

        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);

        NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);
        float red = pixel[0]/255.0;
        float green = pixel[1]/255.0;
        float blue = pixel[2]/255.0;
        float alpha = pixel[3]/255.0;
        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

        [[NSUserDefaults standardUserDefaults] setFloat:red forKey:@"navBarRed"];
        [[NSUserDefaults standardUserDefaults] setFloat:green forKey:@"navBarGreen"];
        [[NSUserDefaults standardUserDefaults] setFloat:blue forKey:@"navBarBlue"];    
        [[NSUserDefaults standardUserDefaults] setFloat:alpha forKey:@"navBarAlpha"];



        return color;
    }





  - (IBAction)handleTapFrom:(id)sender {

        if([sender isKindOfClass:[UITapGestureRecognizer class]])
        {


            CGPoint location = [tapGestureRecognizer locationInView:self.view];

            //remember color preferences
         UIColor* navbarColor =  [self colorOfPoint:location];
            selectColorView.center = location;

            //UPDATE THE NAVBAR COLOR CODE HERE

    //this does not update immediately 
        float navBarRed = [[NSUserDefaults standardUserDefaults] floatForKey:@"navBarRed"];
        float navBarGreen = [[NSUserDefaults standardUserDefaults] floatForKey:@"navBarGreen"];    
        float navBarBlue = [[NSUserDefaults standardUserDefaults] floatForKey:@"navBarBlue"];
        float navBarAlpha = [[NSUserDefaults standardUserDefaults] floatForKey:@"navBarAlpha"];

       UIColor* navBarColor =  [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];


//set the color of all newly created navbars
        [[UINavigationBar appearance] setTintColor:navBarColor];  
        [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
        [[UINavigationBar appearance] setAlpha:0.7];


        //update the toolbar appearance
        [[UIToolbar appearance] setTintColor:navBarColor];

//set the color of the current navbar, displaying immediate change
self.navigationController.navigationBar.tintColor = navbarColor;



        }



    }


推荐答案

难道你不能只设置 NavigationBar tintColor 吗?这应该工作:

Can't you just set the tintColor of the NavigationBar? That should work:

self.navigationController.navigationBar.tintColor = [UIColor redColor];

这篇关于iPhone iOS如何按需重绘UINavigationBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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