基本组件的使用——UITabBarController

和UINavigationController的作用差不多,UITabBarController也可以在多个UIViewController中切换

这个控件的使用相对简单,只需要为该控件的viewControllers添加引用就可以了,然后将根视图控制器设置为该控件即可。如下图所示。

image

最终效果:

UITabBarController

实现代码:

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

//创建第一个视图控制器并添加系统按钮样式
    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类。如果需要自定义这两个按钮,

可以分别设置TabBarItem的图片属性:下图红圈处image

按钮文字则可以直接使用UIViewController的title属性设置。

原文地址:https://www.cnblogs.com/ai-developers/p/4516697.html