2013-01-25 67 views
0

我有一個關於自定義UITabBarItem故事板 - 如何定製UITabbarItem與裏面的UITabBarController UINavigationController的

首先的問題,您可以download my code demo

現在,我定製的UITabBarController在MyAppDelegate是:

-(void)configureiPhoneTabBar 
{ 
    tabViewController = (UITabBarController *)self.window.rootViewController; 
    UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0]; 
    [self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1]; 

    UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1]; 
    [self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2]; 
} 

-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController 
{ 
    UIImage* icon1 = [UIImage imageNamed:imageName1]; 
    UIImage* icon2 = [UIImage imageNamed:imageName2]; 

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0]; 
    [item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] } 
         forState:UIControlStateNormal]; 
    [item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2]; 
    [viewController setTabBarItem:item1]; 
} 

我在UINavigationController裏面使用了一個UITabbarcontroller,並且我無法從appdelegate自定義uitabaritem,如果你運行代碼並在UITabBarController中檢查「initial」,界面顯示爲true,但是當在「in itial「很難做到這一點

回答

0

移動代碼以將標籤欄自定義到LoginViewController(這是一個延伸到標籤欄控制器的標籤欄)。我將此代碼添加到該文件的末尾:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"idenLogin"]) { 
     UITabBarController *tabViewController = segue.destinationViewController; 
     UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0]; 
     [self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1]; 
     UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1]; 
     [self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2]; 
    } 
} 


-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController { 
    UIImage* icon1 = [UIImage imageNamed:imageName1]; 
    UIImage* icon2 = [UIImage imageNamed:imageName2]; 

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0]; 
    [item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] } 
         forState:UIControlStateNormal]; 
    [item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2]; 
    [viewController setTabBarItem:item1]; 
} 
+0

感謝您的快速回復,它工作正常:) – BlueSky

0

如果您從登錄視圖控制器啓動您的應用程序,您可以從該初始viewController配置tabBarController。爲此,您可以在prepareForSegue,因爲你將不得不在segue.destinationViewController的指針tabBarController ...

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
     [self configureiPhoneTabBar:segue.destinationViewController]; 
    } 

移動你的tabBarViewController-配置代碼到這個的viewController ...

-(void)configureiPhoneTabBar:(UITabBarController*)tabViewController 
{ 
     // tabViewController = (UITabBarController *)self.window.rootViewController; 
    UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0]; 
     //...etc... 

或將configureiPhoneTabBar:發送給您的應用程序委託來完成配置(但確實最好將代碼保留在應用程序委託之外)。

更妙的是,你可以繼承UITabBarViewController,把你的配置代碼在那裏,viewDidLoad中觸發:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self configureiPhoneTabBar]; 
} 


-(void)configureiPhoneTabBar 
    { 
    UIViewController *controller1 = [[self viewControllers] objectAtIndex:0]; 

     ...etc... 

作爲另一種選擇,你可以在被加載到標籤欄的相應viewControllers找到配置代碼。

+0

我來自的appdelegate移動功能confiureIphoneTabBar到一個UIViewController並調用它,但依舊不改tabbaritem :) – BlueSky

+0

設法把您的登錄視圖控制器內工作。你只需要移動你的兩個方法,添加prepareforsegue,並註釋掉configureiPhoneTabBar的第一行(我的第一個示例解決方案)。從登錄屏幕的導航控制器啓動應用程序。這確實有用,我已經在你的項目上運行了。如果它不適合你,可以在調試器中輸入一些日誌語句,並且如果卡住了,可能會開始一個新問題。 – foundry

+0

謝謝@他是,我完成 – BlueSky

相關問題