基本组件的使用——UITabBarController

和UINavigationController的做用差很少,UITabBarController也能够在多个UIViewController中切换app

这个控件的使用相对简单,只须要为该控件的viewControllers添加引用就能够了,而后将根视图控制器设置为该控件便可。以下图所示。spa

image

最终效果:3d

UITabBarController

实现代码:code

代码在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中完成blog

//建立第一个视图控制器并添加系统按钮样式
    UIViewController *firstViewController = [[UIViewController alloc] init];
    firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
    firstViewController.view.backgroundColor = [UIColor brownColor];
    
    //建立第二个视图控制器并添加系统按钮样式 
    UIViewController *secondViewController = [[UIViewController alloc] init];
    secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
    secondViewController.view.backgroundColor = [UIColor yellowColor];
    
    //初始化UITabBarController
    self.tabController = [[UITabBarController alloc] init];
    
    //添加TabBarController对两个视图对引用
    self.tabController.viewControllers = @[firstViewController, secondViewController];
    
    //设置根视图控制器为UITabBarController
    self.window.rootViewController = self.tabController;

代码很简单,就是实例化了两个UIViewController,而后使用self.tabController.viewControllers添加两个引用。图片

另外两个TabBarItem都是使用系统的样式。TabBarItem属性UITabBarItem类。若是须要自定义这两个按钮,get

能够分别设置TabBarItem的图片属性:下图红圈处imageit

按钮文字则能够直接使用UIViewController的title属性设置。io

相关文章
相关标签/搜索