ios控件——UITarBarController的简单使用

xiaoxiao2021-02-28  96

UITabBarController是标签控制器,和UINavigationController一样是视图控制器的控制器,是一个容器类。不同的是,标签控制器管理的视图控制器不存在层级关系,而是并列的。因此一个视图控制器一旦被加入标签控制器容器中,只要标签控制器存在,视图控制器就不会被释放。 一、UITabBarController的工作原理 标签控制器中管理的各个视图控制器都是并列结构的,如图所示,点击相应的标签会进行视图控制器的切换 二、UITabBarController的使用 1、步骤: 1)初始化UITabBarController 2)设置UIWindow的rootViewController为UITabBarController 3)创建相应的子视图(ViewController),设置其名称、图标等 4)把子视图添加到UITabBarController中 2、代码示例 .h文件 //创建Window  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; //1.初始化一个tabBar控制器 _tabBar=[[UITabBarController alloc]init];  //2.设置控制器为Window的根控制器 /*注意:如果你设置UITabBarController不是在viewcontroller中或者已经存在主视图了,这时就需要修改主视图,代码如下: APPDelegate * appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:_tabBar]; appdelegate.window.rootViewController = nav; */ self.window.rootViewController=tabBar; //设置导航栏是否隐藏 self.navigationController.navigationBarHidden = NO; self.navigationController.navigationBar.backgroundColor = [UIColor redColor]; //3.创建相应的子视图(ViewController),设置其名称、图标等 //初始化精选界面 _selection = [[SelecViewController alloc] init]; _selection.title = @"精选"; _selection.tabBarItem.image = [UIImage imageNamed:@"selec"]; //初始化目的地界面 _destination = [[DestinViewController alloc] init]; _destination.title = @"新闻"; _destination.tabBarItem.image = [UIImage imageNamed:@"destin"]; //初始化购买界面 _buy = [[BuyViewController alloc] init]; _buy.title = @"动态"; _buy.tabBarItem.image = [UIImage imageNamed:@"buy"]; //初始化我的界面 _my = [[MyViewController alloc] init]; _my.title = @"我的"; _my.tabBarItem.image = [UIImage imageNamed:@"my"]; //4.把子视图添加到UITabBarController中 _tabBar.viewControllers = @[_selection,_destination,_buy,_my]; _tabBar.delegate = self; //tabBar的背景色 _tabBar.tabBar.barTintColor = [UIColor whiteColor]; //点击之后的颜色 _tabBar.tabBar.tintColor = [UIColor blueColor]; .m文件 @property (nonatomic,strong) UITabBarController *tabBar; //四个切换视图 @property (nonatomic,strong) SelecViewController *selection; @property (nonatomic,strong) DestinViewController *destination; @property (nonatomic,strong) BuyViewController *buy; @property (nonatomic,strong) MyViewController *my; 三、效果图 其中UITabBarButton显示什么内容,由对应的子控制器tabBarItem属性来决定
转载请注明原文地址: https://www.6miu.com/read-70030.html

最新回复(0)