UITabBarController 笔记(一)AppDelegate中加UITabBarController 为 rootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //初始化 tabBarItem对应的ViewController
    UIViewController *viewCtrl1 = [[UIViewController alloc] init];
    viewCtrl1.title = @"first viewctrl";
    viewCtrl1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"rename first" image:nil tag:1];//自己定义tabBarItem是在对应的ViewController中去做
    viewCtrl1.view.backgroundColor = [UIColor blueColor];
    
    UIViewController *viewCtrl2 = [[UIViewController alloc] init];
    viewCtrl2.title = @"second viewctrl";
    viewCtrl2.view.backgroundColor = [UIColor yellowColor];
    viewCtrl2.tabBarItem.badgeValue = @"360";
    
    UIViewController *viewCtrl3 = [[UIViewController alloc] init];
    viewCtrl3.title = @"3 viewctrl";
    viewCtrl3.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.2 blue:0.3 alpha:1.0];
    
    UIViewController *viewCtrl4 = [[UIViewController alloc] init];
    viewCtrl4.title = @"4 viewctrl";
    viewCtrl4.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.3 alpha:1.0];
    
    UIViewController *viewCtrl5 = [[UIViewController alloc] init];
    viewCtrl5.title = @"5 viewctrl";
    viewCtrl5.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0];
    
    UIViewController *viewCtrl6 = [[UIViewController alloc] init];
    viewCtrl6.title = @"6 viewctrl";
    viewCtrl6.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.5 blue:0.7 alpha:1.0];
    
    UIViewController *viewCtrl7 = [[UIViewController alloc] init];
    viewCtrl7.title = @"7 viewctrl";
    viewCtrl7.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.5 blue:0.3 alpha:1.0];
    
    //
    tabBarCtrl = [[UITabBarController alloc] init];
    tabBarCtrl.viewControllers = [NSArray arrayWithObjects:viewCtrl1, viewCtrl2 ,viewCtrl5,  viewCtrl3, viewCtrl4, viewCtrl6, viewCtrl7, nil];//添加viewcontrollers, 数组顺序就是tabBarItem对应的viewController顺序
 tabBarCtrl.customizableViewControllers = [NSArray arrayWithObjects: viewCtrl2 ,viewCtrl3, viewCtrl5, viewCtrl7, nil]; //显示可编辑的有那些viewcontroller

  [tabBarCtrl  setSelectedIndex: 3];//设置选中的是那个tabBarItem,如果为无效的index,则默认选中第一个


tabBarCtrl.view.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height); 
self.window.rootViewController = tabBarCtrl; [self.window makeKeyAndVisible]; return YES; }


 

关于旋转

UITabBarController默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。

 

关于 UITabBarControllerDelegate 委托

//设置用户是否可以选中,如果返回NO,用户不能选中,但代码中还可以用tabBarCtrl  setSelectedIndex: 3]选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
    return YES;
}
//选中后的消息响应
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}


 

more 中edit监测

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
原文地址:https://www.cnblogs.com/Free-Thinker/p/5009840.html