Create a Tab Bar
This recipe shows you how to create a Tab Bar in your Xamarin.iOS application.
To add a tab bar to your Xamarin.iOS application:
- Create a subclass of
- Create class-level fields for each
UIViewControllerthat corresponds to a tab.
- In the
ViewDidLoadmethod, instantiate each
UIViewControllerand ensure that a Title has been set to display on the tab bar. In this example each view is given a different color, but in your code each one will be a specific subclass of
UIViewController(or even a UINavigationController) that performs a specific function.
- Create an array of
UIViewControllersand assign that to the
ViewControllersproperty of the
UITabBarController. This creates the items in the tab bar.
- Set which tab is selected when the tab bar is displayed:
- Instantiate the
AppDelegateand make it the root view controller of your application
Tabs should also have an image associated with them, which has been omitted from the code above for clarity. The image is assigned by setting the TabBarItem property of each view controller.
Tab bar images are generally 30x30 pixels in size. The alpha (transparency) values in the source image are used to create the final tab image – blue on black when it appears in the tab bar and black on white if it appears in the More list.
You can use a built-in tab image from the
This automatically sets the tab’s image and text.
tab1.TabBarItem = new UITabBarItem (UITabBarSystemItem.History, 0);
Custom images can be set using a different
tab2.TabBarItem = new UITabBarItem ("l'orange", UIImage.FromFile("Images/first.png"), 1);
TabBarItem’s properties can also be set directly, like this:
tab3.TabBarItem = new UITabBarItem(); tab3.TabBarItem.Image = UIImage.FromFile ("Images/second.png"); tab3.TabBarItem.Title = "Rouge";
TabBarItem also exposes other properties, such as
BadgeValue (which sets
the little red and white number/circle):
tab3.TabBarItem.BadgeValue = "4";
and Enabled, which allows you to disable the tab if required:
tab3.TabBarItem.Enabled = false;
iPhone/iPod devices can only display five tabs in portrait orientation, the iPad can display eight. If you create more tabs than the device can display, the last tab automatically becomes More and the remaining tabs are presented in a list.
While it is common for an iPhone/iPod application to have more than five tabs, Apple discourages more than eight tabs on the iPad. This is because the More list looks very sparse on the larger screen.