[英]setting gradient in a UITabBar background
What is the easiest way that I can achieve something like this: 我能做到这样的最简单的方法是什么:
Is it through subclassing a UITabBarController? 是通过子类化UITabBarController吗? If yes what property needs to be changed?
如果是,需要更改哪些属性?
This is an old question but it comes up first in a search for TabBar gradients. 这是一个老问题,但它首先在搜索TabBar渐变时出现。 A far easier way to do this is with appearances.
更容易实现这一点的方法是外观。 Just put this in your applicationDidFinishLaunching method.
把它放在你的applicationDidFinishLaunching方法中。
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBarImage_1x49"]];
And, as a bonus, if you prefer to create a gradient image in code, you can do this: 而且,作为奖励,如果您更喜欢在代码中创建渐变图像,则可以执行以下操作:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 1, 49);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.darkGrayColor] CGColor], (id)[UIColor.blackColor] CGColor], nil];
UIGraphicsBeginImageContext(gradient.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[gradient renderInContext:context];
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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. 我的应用程序遇到了同样的问题,我想出了以下内容:在applicationDidFinishLaunching方法中,我创建了一个绘制渐变的函数,并使用我的UITabBarController实例根据设备的宽度设置正确的渐变框架。
- (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));
//set up your gradient
//......
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return gradientImage;
}
get the instance of UITabBarController 获取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. 我不确定这是否是正确的方法,但它对我有用。
@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end
@implementation CustomizeUITabBarController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:kMainColor];
[v setAlpha:0.5];
[[self tabBar] addSubview:v];
[v release];
}
@end
Hope it will help you.... 希望它会帮助你......
Yes, subclassing an UITabbarController is the right way to do this. 是的,继承UITabbarController是正确的方法。
Overwrite the drawRect method: 覆盖drawRect方法:
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"yourNavigationBar.png"] drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
}
And add the image with gradient into your project. 并将带有渐变的图像添加到项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.