iOS开发 代码创建UITabBarController

// 代码创建UITabBarController
- (void)addTabBarSubControllers{
    
    // 消息
    UIViewController  *chatController = [[UIViewController  alloc] init];
    chatController.view.backgroundColor = [UIColor whiteColor];
    chatController.title = @"聊天中心"; // 导航条的标题 注意顺序,需要写在tabBarItem.title之前
    chatController.tabBarItem.image = [UIImage imageNamed:@"mainchat"];
    chatController.tabBarItem.title = @"消息";
    UINavigationController *chatNavigation = [[UINavigationController alloc] initWithRootViewController:chatController];
    
    // 联系人
    UIViewController  *contactsController = [[UIViewController  alloc] init];
    contactsController.view.backgroundColor = [UIColor whiteColor];
    contactsController.title = @"联系人";
    contactsController.tabBarItem.image = [UIImage imageNamed:@"contacts"];
    contactsController.tabBarItem.title = @"联系人";
    UINavigationController *contactsNavigation = [[UINavigationController alloc] initWithRootViewController:contactsController];
    
    // 动态
    UIViewController  *dynamicController = [[UIViewController  alloc] init];
    dynamicController.view.backgroundColor = [UIColor whiteColor];
    dynamicController.title = @"动态";
    dynamicController.tabBarItem.image = [UIImage imageNamed:@"dynamic"];
    dynamicController.tabBarItem.title = @"动态";
    UINavigationController *dynamicNavigation = [[UINavigationController alloc] initWithRootViewController:dynamicController];
    
    //创建tabbarcontroller来管理这些页面
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = @[chatNavigation, contactsNavigation, dynamicNavigation];
    self.view.window.rootViewController = tabBarController;
    
}

你可能感兴趣的:(ios)