[英]setting gradient in a UITabBar background
我能做到這樣的最簡單的方法是什么:
是通過子類化UITabBarController嗎? 如果是,需要更改哪些屬性?
這是一個老問題,但它首先在搜索TabBar漸變時出現。 更容易實現這一點的方法是外觀。 把它放在你的applicationDidFinishLaunching方法中。
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBarImage_1x49"]];
而且,作為獎勵,如果您更喜歡在代碼中創建漸變圖像,則可以執行以下操作:
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();
我的應用程序遇到了同樣的問題,我想出了以下內容:在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;
}
獲取UITabBarController的實例
UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;
設置你的漸變
[UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];
我不確定這是否是正確的方法,但它對我有用。
@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
希望它會幫助你......
是的,繼承UITabbarController是正確的方法。
覆蓋drawRect方法:
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"yourNavigationBar.png"] drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
}
並將帶有漸變的圖像添加到項目中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.