关于iPhone:移除导航栏和按钮上的白色渐变

Remove White Gradient on Navigation Bar and Button

在Mainstoryboard和Simulator中,导航栏和按钮上的颜色为纯色。但是,如果您在真实的iPhone或iPad上运行,则会得到带有颜色的白色渐变。有没有办法将其删除。

enter


在ios5及更高版本中,

您可以使用协议轻松地对其进行自定义。现在,所有视图控制器和UI元素都符合此协议。 UIView中通常有两种不同类型的方法,可以通过(id)appearance或(id)appearanceWhenContainedIn:(Class)ContainerClass访问UIAppearance协议。

使用第一种方法,您一次只能自定义一个导航栏。因此,如果要自定义导航栏的所有实例,请使用

[[UINavigationBar外观] setTintColor:myColor];

或者,如果您要基于其位置和放置位置来设置自定义navigationBar,通常应使用

1
2
[[UINavigationBar appearanceWhenContainedIn:[UIViewController class], nil]
       setTintColor:myNavBarColor];

这将修改视图控制器内部的所有现有导航控制器。

但是在以前的ios5中,为某些特定的视图控制器设置导航栏色调颜色甚至更加容易,并且只是一行代码;

1
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

但是,如果您创建的导航栏不是导航控制器的一部分,而只是视图控制器的一部分,那么请为其保留一个出口并按上面的方式自定义它,或者,

1
[self.navigationBar setTintColor:[UIColor violetColor]];

在viewDidLoad;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UIImage *backgroundImage = [self drawImageWithColor:[UIColor blueColor]];
 [self.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];


-(UIImage*)drawImageWithColor:(UIColor*)color{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *imagePath;
    imagePath = [[paths lastObject] stringByAppendingPathComponent:@"NavImage.png"];
    if([fileManager fileExistsAtPath:imagePath]){
      return  [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
    }
    UIGraphicsBeginImageContext(CGSizeMake(320, 40));
    [color setFill];
    UIRectFill(CGRectMake(0, 0, 320, 40));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:imagePath atomically:YES];
    return image;
}