本文简单介绍两个使用示例及代码。
示例一
代码方式 创建 UITabBarController , 在 UITabBarController 控件上放两个视图控制器。
Demo:
文档组织结构
实现步骤
1️⃣ 新建两个父类为 UIViewController 的带 xib 的视图控制器,MainViewController 和 LoginViewController
2️⃣ AppDelegate.h:
#import <UIKit/UIKit.h>
// 导入创建的视图控制器头文件
#import "MainViewController.h"
#import "LoginViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
// 添加选项卡控制器属性,用来展示组合并展示创建的两个视图控制器
@property (strong, nonatomic) UITabBarController *tabBarController;
@end
- 3️⃣ AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建 Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 实例化视图控制器对象
UIViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UIViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
// 初始化选项卡视图控制器
self.tabBarController = [[UITabBarController alloc] init];
// 将两个视图控制器以数组的方法指定给选项卡视图控制器
NSArray *viewControllerArrays = [NSArray arrayWithObjects:mainViewController,loginViewController, nil];
self.tabBarController.viewControllers = viewControllerArrays;
// 设置选项卡视图控制器为当前窗口的根视图控制器
self.window.rootViewController = _tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
// 设置 Window 为主窗口并显示
[self.window makeKeyAndVisible];
return YES;
}
- 4️⃣ 设置每个视图控制器标签的标题和图标
MainViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"主页";
self.tabBarItem.image = [UIImage imageNamed:@"1"];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
return self;
}
LoginViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"登录";
self.tabBarItem.image = [UIImage imageNamed:@"4"];
[self.view setBackgroundColor:backgroundColor];
}
return self;
}
- 完成。
示例二:自定义 UITabBarController 子类对象:
- (instancetype)init {
self = [super init];
if (self) {
// 设置背景颜色和大小
self.view.backgroundColor = COLOR_BACKGROUND;
self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
[self setViewControllers];
}
return self;
}
/*
* 视图堆栈结构:
* UITabBarController - UINavigationController - UIViewController (首页)
* - UINavigationController - UIViewController (资讯)
* - UINavigationController - UIViewController (我的)
*
*/
- (void)setViewControllers {
// 首页 TabBar
UIViewController *mainViewController = [self renderTabBarItem:[[HQLMainViewController alloc] init] title:@"首页" imageNamed:@"tab_home_normal" selectedImageNamed:@"tab_home_selected"];
HQLNavigationController *mainNavigationController = [[HQLNavigationController alloc] initWithRootViewController:mainViewController];
// 资讯 TabBar
UIViewController *secondViewController = [self renderTabBarItem:[[HQLSecondViewController alloc] init] title:@"资讯" imageNamed:@"tab_news_normal" selectedImageNamed:@"tab_news_selected"];
HQLNavigationController *secondNavigationController = [[HQLNavigationController alloc] initWithRootViewController:secondViewController];
// 我的 Tabbar
UIViewController *thirdViewController = [self renderTabBarItem:[[HQLThirdViewController alloc] initWithStyle:UITableViewStyleGrouped] title:@"我的" imageNamed:@"tab_mine_normal" selectedImageNamed:@"tab_mine_selected"];
HQLNavigationController *thirdNavigationController = [[HQLNavigationController alloc] initWithRootViewController:thirdViewController];
self.viewControllers = @[mainNavigationController,secondNavigationController,thirdNavigationController];
}
- (UIViewController *)renderTabBarItem:(UIViewController *)viewController
title:(NSString *)title
imageNamed:(NSString *)normalImgName
selectedImageNamed:(NSString *)selectedImgName {
viewController.title = title;
UIImage *normalImage = [[UIImage imageNamed:normalImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:selectedImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:normalImage selectedImage:selectedImage];
[viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: COLOR_THEME}
forState:UIControlStateSelected];
return viewController;
}