Reputation:
In Mainstoryboard and the Simulator you get a plain color on the Navigation Bar and Button. But if you run on a real iPhone or iPad you get this white gradient with your color. Is there a way to remove it.
iPhone Image Improved Image
Upvotes: 3
Views: 4819
Reputation: 21144
in ios5 and later you could easily customise it using protocol. All the view controller and UI Elements now conform to this protocol. There are typically two different type of method in UIView that can access you to UIAppearance protocol either + (id)appearance or + (id)appearanceWhenContainedIn:(Class )ContainerClass.
With the first method you can customise only one navigation bar at a time. So, if you want to customise all the instances of navigation bar you would use;
[[UINavigationBar appearance] setTintColor:myColor];
Or if you want to set customize navigationBar based on its position and where it is placed you would normally use,
[[UINavigationBar appearanceWhenContainedIn:[UIViewController class], nil]
setTintColor:myNavBarColor];
This would modify all the existing navigation controller that are inside the view controller.
But prior ios5, it is even easier to set navigation bar tint color for some specific view controller and it is just a line of code as;
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
But, if you create the navigation bar which is not a part of navigation controller but just a view controller then keep an outlet to it and customise it as above or,
[self.navigationBar setTintColor:[UIColor violetColor]];
In 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;
}
Upvotes: 9