iPhone iOS how to redraw UINavigationBar on demand

2019-03-22 08:07发布

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条回答
不美不萌又怎样
2楼-- · 2019-03-22 08:38

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

self.navigationController.navigationBar.tintColor = [UIColor redColor];
查看更多
登录 后发表回答