iPhone iOS how to redraw UINavigationBar on demand?

久未见 提交于 2019-12-04 14:38:19

问题


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.

This does not work:

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.

What's the correct way to ask the UINavigationBar to redraw itself on demand?

Thank you!

UPDATE: Here's the code that works!

    - (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;



        }



    }

回答1:


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

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


来源:https://stackoverflow.com/questions/9484741/iphone-ios-how-to-redraw-uinavigationbar-on-demand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!