简体   繁体   中英

Storyboard - How to custom UITabbarItem with UITabbarController inside UINavigationController

I have a problem about custom UITabBarItem

First of all, you can download my code demo

Now, I'm custom UITabbarcontroller at MyAppDelegate is:

-(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];
}

I'm using a UITabbarcontroller inside UINavigationController, and I can't custom uitabaritem from appdelegate, if you run code and check "initial" in UITabBarController, interface show true, but when check at "initial" in UINavigationController is very difficult to do so

If you start your app from the login view controller, you could configure the tabBarController from that initial viewController. You can do this in a prepareForSegue , as there you will have a pointer to the tabBarController in segue.destinationViewController ...

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

Move your tabBarViewController-configuring code into this viewController...

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

or send configureiPhoneTabBar: to your app delegate to do the configuring (but really it is best to keep code out of your app delegate).

Better still you could subclass UITabBarViewController and put your configuration code in there, triggered by viewDidLoad:

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


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

        ...etc...

As another option, you could locate the config code in the respective viewControllers that are loaded into the tab bar.

Move the code to customize the tab bar to the LoginViewController (which is the one that segues to the tab bar controller). I added this code to the end of that file:

-(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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM