I’m making an app with interface builder using storyboarding.
I want to have a tab bar where no item is selected. This can be accomplished by setting
TabBar.SelectedItem = null;
But if you try to do that, you get the following error:
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
So I can’t use the standard UITabBarController. I’ve created a custom UIViewController, and added a UITabBar. Switching between tabs is working fine, and having no selection is also working as it should.
But I have no idea how to show my other view controllers from my custom view controller with the tab bar. Remember that I’m using interfacebuilder, so I can’t just create my view controllers in code as new objects and add them to the view. (as suggested in UITabBar funcionality without UITabBarController)
So how do I show my own views without using the UITabBarViewController?
Edit: Still haven’t found a solution, but I did a hacky fix. Simple create an other tab bar and place it on top of the original tab bar. Listen to those events and use SelectedIndex to change the view displayed. Then add some function that will select / deselect the items on your own tab bar.
In fact, even if you design your others
UIViewControllersfrom IB, you can instantiate them from code. You’ll probably have to play a bit with frame and autoresizing properties to make them fit the part of your main view you want them to display inside, but it’s possible.So, knowing that, a simple solution is to create a simple
UIView(we’ll call it ‘tabFrame’) in your mainUIViewController, which fill the screen from the top of yourUITabBarto the top of the screen; instantiate theUIViewControllercorresponding to your tabs and add their view as subview oftabFrame. Now you just have to catch item selection from tabbar to hide or show the desired subviews.Hope I’m clear enough, else don’t hesitate to question!
EDIT: pointed out this morning that in storyboarding context, you can effectively instantiate viewControllers / scene from code, but for not loosing designer settings it must NOT be done through directly calling their constructors, but through
StoryBoard.InstantiateViewController("vc_identifier")calls, after having set identifiers to VCs in storyboard editor.See http://docs.xamarin.com/ios/recipes/General/Storyboard/Storyboard_a_TableView for example