iOS 开发之 菜单栏UITabBarController

来源:互联网 发布:斯芬克 知乎 编辑:程序博客网 时间:2024/06/05 03:45


1、菜单栏 : UITabBarController 提供选择进入哪一个页面 ,也属于UIViewControl

             通过一个数组,里面存放的是视图控制器

2、初始化菜单栏 通过改变轨道的颜色,来改变按钮的颜色

UITabBarController *tabBarController = [[UITabBarController alloc]init];

tabBarController.view.tintColor = [UIColor blackColor];

3、显示3个菜单栏

tabBarController.viewControllers = @[@“grupe”,@“news”,@“userInfo”];

NSArray *title = @[@“圈子”,@“新闻”,@“个人中心”];

for(int i =0, i <3 , i ++){

UITabBarItem *allItem = tabBarController.tabBar.items[i];

allItem.title = title[i];

allItem.image = [UIImage imageName:title[i]];

}

tabBarController.selectedIndex  = 0;//默认选项

self.window.rootViewController = tabBarController;

4、带导航控制器的菜单栏

1⃣️每初始化一个ViewController,就得初始化一个对应的导航控制器

ViewController *vc = [[ViewController alloc]init];

UINavigationController *nv = [[UINavigationController alloc]initWithRootViewCotroller:vc];

2⃣️菜单栏的所有视图是初始化的导航控制器,如

tabBarCotroller.viewControllers = @[nv,nv1,nv2];



代码如下:

<span style="font-size:14px;">self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];        ViewController *grupe = [[ViewController alloc]init];    NewsViewController *news = [[NewsViewController alloc]init];    UserinfoViewController *userInfo = [[UserinfoViewController alloc]init];        UITabBarController *tabBarController = [[UITabBarController alloc]init];//    通过改变tabBarController 的轨道的颜色,来改变 按钮的颜色    tabBarController.view.tintColor = [UIColor blackColor];        tabBarController.viewControllers = @[grupe,news,userInfo];        NSArray *title = @[@"圈子",@"新闻",@"个人中心"];    for (int i = 0; i<3; i++) {        UITabBarItem *allItem = tabBarController.tabBar.items[i];        allItem.title = title[i];        allItem.image = [UIImage imageNamed:title[i]];    }    tabBarController.selectedIndex = 1;//默认选项    self.window.rootViewController = tabBarController;    [self.window makeKeyAndVisible];</span>




0 0