iOS 通过Storyboard 自定义 UITabBarController

通过Storyboard 和代码结合自定义创建 UITabBarController

1.创建如下几个控制器 和 继承于 UITabBarController 的 MainTabBarController 以及 四个 storyboard
屏幕快照 2018-09-21 下午2.55.30.png
屏幕快照 2018-09-21 下午2.55.43.png
2. 在 MainTabBarController 设置 Controller
-(void)initTabBarController
{
    // 首页
    UIImage *homeNormal = [[UIImage imageNamed:@"home_tabbar_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *homeSelect = [[UIImage imageNamed:@"home_tabbar_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIStoryboard *homeSB = [UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]];
    UINavigationController *homeNav = [homeSB instantiateInitialViewController];
    homeNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:homeNormal selectedImage:homeSelect];
    
    // 视频
    UIImage *videoNormal = [[UIImage imageNamed:@"video_tabbar_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *videoSelect = [[UIImage imageNamed:@"video_tabbar_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIStoryboard *videoSB = [UIStoryboard storyboardWithName:@"Video" bundle:[NSBundle mainBundle]];
    UINavigationController *videoNav = [videoSB instantiateInitialViewController];
    videoNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视频" image:videoNormal selectedImage:videoSelect];
    
    // 文章
    UIImage *articleNormal = [[UIImage imageNamed:@"huoshan_tabbar_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *articleSelect = [[UIImage imageNamed:@"huoshan_tabbar_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIStoryboard *articleSB = [UIStoryboard storyboardWithName:@"Article" bundle:[NSBundle mainBundle]];
    UINavigationController *articleNav = [articleSB instantiateInitialViewController];
    articleNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"文章" image:articleNormal selectedImage:articleSelect];
    
    // 消息
    UIImage *messageNormal = [[UIImage imageNamed:@"weitoutiao_tabbar_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *messageSelect = [[UIImage imageNamed:@"weitoutiao_tabbar_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIStoryboard *messageSB = [UIStoryboard storyboardWithName:@"Message" bundle:[NSBundle mainBundle]];
    UINavigationController *messageNav = [messageSB instantiateInitialViewController];
    messageNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:messageNormal selectedImage:messageSelect];
    
    self.viewControllers = @[homeNav,videoNav,articleNav,messageNav];
}
3. 然后在 AppDelegate 里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    MainTabBarController *mainTabBarController = [[MainTabBarController alloc] init];
    self.window.rootViewController = mainTabBarController;
    
    return YES;
}

你可能感兴趣的:(iOS 通过Storyboard 自定义 UITabBarController)