默認的UITabBarController的tabBar背景色是黑色的。有時我們的應用需要變更背景色,或用指定圖片做為背景圖。
如下有兩處實現方法, 這兩種方法均需要先實例UITabBarController。
如app的創建的是基於UITabBarController的應用,可將如下代碼加入到AppDelegate 的委托方法didFinishLaunchingWithOptions: 中
方法一:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
CGRect frame = CGRectMake(0, 0, 320, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *img = [UIImage imageNamed:@"tabbar.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:img];
v.backgroundColor = color;
[tabBarController.tabBar insertSubview:v atIndex:0];
tabBarController.tabBar.opaque = YES;
[color release];
[v release];
方法二:
默認UITabBarController的tabBar背景是黑色的。可以按 下面方法:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *array = [tabBarController.view subviews];
UITabBar *tabBar = [array objectAtIndex:1];
UIImage *image = [UIImage imageWithContentsOfFile:sourcePath ];
tabBar.layer.contents = (id)image.CGImage;
個人推薦使用第二種方法。代碼簡潔,思路清晰
