1.AppDelegate设置
新建MRRootTabBarController继承UITabBarViewController
添加头文件:#import "MRRootTabBarController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
application.statusBarHidden = NO;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[MRRootTabBarController alloc]init];
[self.window makeKeyAndVisible];
return YES;
}
2.MRRootTabBarController中添加四个自定义的UITableViewController
#import "MRRootTabBarController.h"
#import "MRHomeViewController.h"
#import "MRMessageViewController.h"
#import "MRDiscoverViewController.h"
#import "MRMeViewController.h"
@interface MRRootTabBarController ()
@end
@implementation MRRootTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化所有的子控制器
[self setupAllChildViewControllers];
// Do any additional setup after loading the view.
}
/**
* 初始化所有的子控制器
*/
- (void)setupAllChildViewControllers
{
// 1.首页
MRHomeViewController *home = [[MRHomeViewController alloc] init];
[self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
// 2.消息
MRMessageViewController *message = [[MRMessageViewController alloc] init];
[self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
// 3.广场
MRDiscoverViewController *discover = [[MRDiscoverViewController alloc] init];
[self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
// 4.我
MRMeViewController *me = [[MRMeViewController alloc] init];
[self setupChildViewController:me title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}
/**
* 初始化一个子控制器
*
* @param childVc 需要初始化的子控制器
* @param title 标题
* @param imageName 图标
* @param selectedImageName 选中的图标
*/
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
// 1.设置控制器的属性
childVc.title = title;
childVc.tabBarItem.image = [UIImage imageNamed:imageName];
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 2.包装一个导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
[self addChildViewController:nav];
}
@end
最终显示的结果图: