删除导航栏和按钮上的白色渐变

Pro*_*... 3 iphone xcode objective-c uinavigationbar ipad

在Mainstoryboard和Simulator中,您可以在导航栏和按钮上获得纯色.但是如果你在真正的iPhone或iPad上运行,你会得到这种带有颜色的白色渐变.有没有办法删除它.

在此输入图像描述iPhone图片 改进的图像

San*_*eep 10

在ios5及更高版本中,您可以使用协议轻松自定义它.现在,所有视图控制器和UI元素都符合此协议.在UIView中通常有两种不同类型的方法可以访问UIAppearance协议+(id)外观+(id)appearanceWhenContainedIn :( Class)ContainerClass.

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

[[UINavigationBar外观] setTintColor:myColor];

或者,如果您想根据自己通常使用的位置和位置设置自定义navigationBar,

[[UINavigationBar appearanceWhenContainedIn:[UIViewController class], nil]
       setTintColor:myNavBarColor];
Run Code Online (Sandbox Code Playgroud)

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

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

[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

但是,如果您创建的导航栏不是导航控制器的一部分,而只是一个视图控制器,那么请保留一个插座并按上述方式对其进行自定义,或者,

[self.navigationBar setTintColor:[UIColor violetColor]];
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中;

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;
}
Run Code Online (Sandbox Code Playgroud)