简体   繁体   中英

iOS - Is it still possible to dynamically add items to UITabBarController in this age of adaptive layout?

I have an app that communicates with an outside entities. I need to either show or hide an item on the tab bar if that entity has a switch flipped on or off.

It has to be dynamic in case the user changes from entity A to entity B and those have different settings. Tab bar item needs to show/hide if they switch and settings differ.

I used to add an item to a UITabBarController like this:

UIViewController *vc1 = [[MyViewController1 alloc] init];
[self.tabBarController addChildViewController:vc1];

UIViewController *vc2 = [[MyViewController2 alloc] init];
[self.tabBarController addChildViewController:vc2];

And I could remove items from a tab bar like this:

    NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
    [tbViewControllers removeObjectAtIndex:4];
    [tbViewControllers removeObjectAtIndex:4];
    [self.tabBarController setViewControllers:tbViewControllers];

In my ViewController Code I had something like this:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        //get the tab bar item
        UITabBarItem *tbi = [self tabBarItem];

        //give it a label
        [tbi setTitle:@"Tab Item"];

        //create a UIImage from a file
        UIImage *i = [UIImage imageNamed:@"image.png"];

        //put that image on the tab bar item
        [tbi setImage:i];
    }
    return self;
}

Now, with the advent of adaptive app layout and segues it seems that adding an item dynamically does not work. At least for me.

I get a black screen instead of the view I expect to see when doing the above. The UIView in question is basically a UIWebView with a couple custom buttons above it.

My app is fully adaptive layout compatible with UIStackViews and whatnot.

All the posts I see here referencing adding or removing items from UITabBars are all several years old, before modern adaptive layout.

Is it still possible to add/remove tab items from the tab bar anymore? Is it advisable to do so or is this now bad practice?

Any other ideas to accomplish this same goal of having an item on a tab bar or not depending on an outside setting?

Cheers,

TJ

I found my own solution and am sharing it to help others.

The trick was how the UIViewController was created.

The new way to do this is to create the view controller by instantiating from the storyboard.

I found the idea from this question .

Creating and adding to the TabView is done like this:

// Get a reference to the Tab View Controllers array
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];

// Get a reference to the storyboard                    
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

// Instantiate a new instance of the ViewController
UIViewController *vc1 = [sb instantiateViewControllerWithIdentifier:@"MyVC"];

// Add the new view controller to the array at the desired location
[tbViewControllers insertObject:vc1 atIndex:tbViewControllers.count-1];

// Set the tab bar controllers to the newly augmented array
[self.tabBarController setViewControllers:tbViewControllers];

That's it. The view controller works as I expect it to. No more black screen.

Hope this helps someone else.

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