0
我可以設置背景一次,但在此之後它不會再次更改。我已經看到了stackoverflow上的所有例子。代碼示例看起來總是一樣的。我已經設置了委託。圖像都可以。我已經將它們設置爲默認圖像,並顯示。但是,應用程序完成啓動後,背景中再也沒有任何事情發生。當點擊項目時TabBar上的setBackgroundImage不起作用
這裏是我的代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
// Override point for customization after application launch.
self.tabController = (UITabBarController *)self.window.rootViewController;
self.tabController.delegate = self;
...
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
int tabitem = self.tabController.selectedIndex;
NSLog(@"tabitem: %i", tabitem);
[self switchTabBarImage:tabitem];
//[[tabController objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}
- (void)switchTabBarImage:(int)selectedIndex
{
NSLog(@"selected: %i", selectedIndex);
if (selectedIndex == 0) {
NSLog(@"0");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 1) {
NSLog(@"1");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-2.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 2) {
NSLog(@"2");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-3.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
if (selectedIndex == 3) {
NSLog(@"3");
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-4.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
}
- (void)customizeInterface
{
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
UIImage *selectionIndicator = [UIImage imageNamed:@"tabbar-icon-clean.png"];
[[UITabBar appearance] setSelectionIndicatorImage:selectionIndicator];
}
調試器顯示:
2012-11-13 02:42:06.147 soundapp[9060:c07] tabitem: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] selected: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] 1
2012-11-13 02:42:07.739 soundapp[9060:c07] tabitem: 2
2012-11-13 02:42:07.739 soundapp[9060:c07] selected: 2
2012-11-13 02:42:07.740 soundapp[9060:c07] 2
我在尋找,也沒有幾個小時,想不通爲什麼它只有一次。有人在我的代碼中看到錯誤嗎?