自定義UITabBar的背景圖片或者顏色


默認的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;

 

個人推薦使用第二種方法。代碼簡潔,思路清晰


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM