- iOS Development with Xamarin Cookbook
- Dimitris Tavlikos
- 506字
- 2021-07-16 11:45:10
Providing controllers in tabs
In this recipe, we will learn how to display multiple view controllers in a tabbed interface.
Getting ready
The UITabBarController
class provides a way to display different view controllers on the same hierarchy level divided into a tab-like interface. Create a new iPhone Empty Project in Xamarin Studio and name it TabControllerApp
.
How to do it…
Perform the following steps to provide controllers in tabs:
- Add two iPhone view controllers to the project. Name them
MainController
andSettingsController
. - Add the following code to the
ViewDidLoad
method ofMainController
:this.View.BackgroundColor = UIColor.Blue;
- Add the following code to the
ViewDidLoad
method ofSettingsController
:this.View.BackgroundColor = UIColor.Yellow;
- Add the following code to the
FinishedLaunching
method of theAppDelegate
class:MainController mainController = new MainController(); SettingsController settingsController = new SettingsController(); UITabBarController tabController = new UITabBarController(); tabController.SetViewControllers(new UIViewController[] { mainController, settingsController }, true); tabController.TabBar.Items[0].Title = "Main"; tabController.TabBar.Items[1].Title = "Settings"; window.RootViewController = tabController;
- Run the app on the simulator. Click on each of the tabs at the bottom. The interface should be similar to the following screenshot when
MainController
is selected:
How it works...
The UITabBarController
class displays one tab for each of the controllers it manages. That tab is of the UITabBarItem
type that can accept both text and images. We set the controllers it will display through its SetViewControllers
property, as follows:
tabController.SetViewControllers(new UIViewController[] { mainController, settingsController }, true);
After we have added the controllers, we can access its tab bar items through the TabBar
property. In this case, we set the tab's Title
attribute:
tabController.TabBar.Items[0].Title = "Main";
Each UIViewController
contains a TabController
property. Similar to the NavigationController
property, when the controller is part of a tab controller, the property will return the instance of that tab controller.
There's more...
The controller can accept as many controllers as we want but if we add six or more, four will be displayed with their tabs, while a fifth predefined More tab will represent all the remaining controllers. This is to keep the interface easily accessible to the user by keeping the tabs to a specific size suitable for human fingers. When we add more than six controllers in a tab bar controller interface, by default, the object provides an Edit button on top in the More tab that allows the user to rearrange the order of controllers. If we want to exclude some controllers from this functionality, we have to remove it from the CustomizableViewControllers
array.
Some more useful properties of the UITabBarController
class are as follows:
ViewControllers
: This is an array containing all the controllers that the tab controller holds.SelectedIndex
: This is the zero-based index of the selected tab. Setting this property to the desired index programmatically selects the corresponding controller.SelectedViewController
: This is the currently selected controller.
See also
- The Using view controllers efficiently recipe
- 數(shù)字媒體應(yīng)用教程
- Ceph Cookbook
- React Native Cookbook
- Mastering Scientific Computing with R
- Python王者歸來
- Oracle數(shù)據(jù)庫從入門到運維實戰(zhàn)
- Full-Stack Vue.js 2 and Laravel 5
- Learning Selenium Testing Tools(Third Edition)
- Python機器學(xué)習(xí)算法與實戰(zhàn)
- Windows內(nèi)核編程
- Spring Boot+MVC實戰(zhàn)指南
- 深度學(xué)習(xí)原理與PyTorch實戰(zhàn)(第2版)
- Android應(yīng)用開發(fā)實戰(zhàn)(第2版)
- 算法圖解
- The Statistics and Calculus with Python Workshop