I want to set background color for NavigationBar and TabBar. It must be a gradient containing two hexadecimal colors. How can I made it in objective-c? Thanks,
For the TabBar I had the same problem with my app and I came up with following: in applicationDidFinishLaunching method I created a function to draw a gradient and used an instance of my UITabBarController to set up a correct gradient frame depending on the width of the device.
- (UIImage *)drawGradientInView:(UITabBarController *) tabBarVC {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(CGRectGetMinX(tabBarVC.tabBar.frame), CGRectGetMinY(tabBarVC.tabBar.frame), CGRectGetWidth(tabBarVC.view.frame), CGRectGetHeight(tabBarVC.tabBar.frame));
gradient.colors = @[(__bridge id)[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
gradient.startPoint = CGPointMake(0.0, 0.5);
gradient.endPoint = CGPointMake(0.5, 0.5);
UIGraphicsBeginImageContext(gradient.bounds.size);
[gradient renderInContext:UIGraphicsGetCurrentContext()];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return gradientImage;
}
get the instance of UITabBarController
UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;
set your gradient
[UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];
I'm not sure if that's a correct approach but it did work for me.
For NavigationBar, I subclassed it and customised that in layoutSubviews
- (void)layoutSubviews {
[super layoutSubviews];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + CGRectGetHeight([UIApplication sharedApplication].statusBarFrame));
gradient.colors = @[(__bridge id)[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
gradient.startPoint = CGPointMake(0.0, 0.5);
gradient.endPoint = CGPointMake(0.5, 0.5);
UIGraphicsBeginImageContext(gradient.bounds.size);
[gradient renderInContext:UIGraphicsGetCurrentContext()];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
Hope this will help you..
This will help you to change the color of Navigation bar & Tab bar
UINavigationController *navigationController;
...
navigationController.navigationBar.tintColor = [UIColor blackColor];
UITabBarController *tabBarCon;
...
tabBarCon.tabBar.tintColor = [UIColor blueColor];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.