http://www.jianshu.com/p/bd95c56b3ff9
1.窗口的根控制器为TabBar控制器
实现
appDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1、创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//2.设置窗口的 根控制器
self.window.rootViewController = [[PDtabBarViewController alloc] init];
//添加子控制器(在TabBar中加)
//3.让窗口显示
[self.window makeKeyAndVisible];
return YES;
}
在TabBar控制器.m中
- 添加所有子控制器
-(void)addAllChildVC{
//1.首页
PDmainRootVC *mainPageVC = [[PDmainRootVC alloc] init];
UINavigationController *nav = [[PDNavigationVC alloc] initWithRootViewController:mainPageVC];
[self addChildViewController:nav];
//设置子控制器的TabBarItem,并给子控制器绑定Nav控制器,同时把Nav添加到TabBar控制器的childcontrollers 中
[self setTabbarContent:mainPageVC image:[UIImage imageNamed:@"首页"] selectImg:[UIImage imageNamed:@"首页(选中)"] title:@"首页"];
//2.限时抢购
PDRushBuyRootVC *rushBuyVC = [[PDRushBuyRootVC alloc] init];
UINavigationController *rushBuyNav = [[PDRushBuyNavigationVC alloc] initWithRootViewController:rushBuyVC];//添加 导航控制器
[self addChildViewController:rushBuyNav]; //把导航控制器 作为TabBar控制器的子控制器
[self setTabbarContent:rushBuyVC image:[UIImage imageNamed:@"抢购"] selectImg:[UIImage imageNamed:@"抢购(选中)"] title:@"限时抢购"];
//3.我的界面
PDmineViewController *mineVC = [[PDmineViewController alloc] init];
PDmineNavigationVC *mineNav = [[PDmineNavigationVC alloc] initWithRootViewController:mineVC];
[self addChildViewController:mineNav];
[self setTabbarContent:mineVC image:[UIImage imageNamed:@"我的"] selectImg:[UIImage imageNamed:@"我的(选中)"] title:@"我的"];
}
- 抽取方法,设置TabBar的内容
-(void)setTabbarContent:(UIViewController *)VC image:(UIImage *)image selectImg:(UIImage *)selectImg title:(NSString *)title{
VC.tabBarItem.title = title;//设置title
[VC.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0]} forState:UIControlStateNormal]; //设置title的字体
VC.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//设置图标
VC.tabBarItem.selectedImage = [selectImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//设置选中图标
}
- 在NavigationController.m中统一设置navBar的内容
//当 当前类第一次初始化的时候调用,但是如果有子类,会调用多次
+(void)initialize{
//如果是在当前类,则执行以下设置
if(self == [PDNavigationViewController class]){
//1.获取APP的导航条标识
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[PDNavigationViewController class],nil]; //获取某几个类的导航条标识
//2.通过标识来设置内容
//2.1 设置nav bar的背景图片(因无法设置背景颜色)
[bar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];
//2.2标题字体大小、颜色
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:22.0];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
[bar setTitleTextAttributes:dict];
//2.3设置导航条前景色
[bar setTintColor:[UIColor whiteColor]];
//3.修改 返回按钮的标题 的位置
UIBarButtonItem *it = [UIBarButtonItem appearanceWhenContainedIn:self,nil];
[it setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
}
}
2.窗口的根控制器为Nav控制器
做法
在appdalegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.创建窗口
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//2.创建导航控制器
AHTabBarViewController *tabBarVC = [[AHTabBarViewController alloc] init];
AHNavViewController *navigationVC = [[AHNavViewController alloc]
initWithRootViewController:self.tabBarVC];
//3.设置窗口的根控制器(为导航控制器
self.window.rootViewController = navigationVC ;
//4.显示窗口
[self.window makeKeyAndVisible];
return YES;
}
在TabBar控制器的.m中,创建子控制器,并设置子控制器的TabBarItem 的内容
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建所有子控制器
//创建子控制器1
PDnearbyViewController *nearby = [[PDnearbyViewController alloc]init];
//设置子控制器的TabBarItem的内容
nearby.tabBarItem.title = @"附近的人";
nearby.tabBarItem.selectedImage = [UIImage imageNamed:@"nearby"];
nearby.tabBarItem.image = [UIImage imageNamed:@"nearby"];
//创建子控制器2
AHDynamicViewController *dynamic = [[AHDynamicViewController alloc] init];
dynamic.tabBarItem.title = @"动态";
dynamic.tabBarItem.selectedImage = [UIImage imageNamed:@"dynamic"];
dynamic.tabBarItem.image = [UIImage imageNamed:@"dynamic"];
//创建子控制器3
AHAddViewController *add = [[AHAddViewController alloc] init];
add.tabBarItem.title = @"添加";
add.tabBarItem.selectedImage = [UIImage imageNamed:@"activity"];
add.tabBarItem.image = [UIImage imageNamed:@"activity"];
//创建子控制器4
。
。同上
。
//2.把创建的所有子控制器 添加到TabBar控制器的子控制器数组中
self.viewControllers = @[nearby,
dynamic,
add,
message,
my
];
self.title = collectionVC.tabBarItem.title; //初始时导航控制器的标题
self.delegate = self;
}
另外,导航控制器的TabBar内容在它的.m中统一设置
。。。