我有使用外观属性更改导航栏颜色的代码。我想立即看到导航栏颜色的变化。但是,我找不到正确的调用来重绘导航栏。当我按下模态导航控制器时,它的颜色会发生变化,从而覆盖了导航栏。
这不起作用:
AppDelegate* appDelegate = ((AppDelegate*)[[UIApplication sharedApplication] delegate]);
[appDelegate.window setNeedsDisplay];
我也试过要求导航控制器的各种实例重绘他们的视图,这些视图不起作用。
要求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;
}
}
答案 0 :(得分:16)
您不能只设置 NavigationBar 的 tintColor 吗?这应该有效:
self.navigationController.navigationBar.tintColor = [UIColor redColor];