最终效果图:
Main.storyboard
初始化的控制器是:导航控制器
它的根控制器是:TabBarController
TabBarController的底部是一个自定义的TabBar
里面添加了5个TabBarItem
点击每一个item,
会将tabBar上的对应item的子控制器的navigationItem的值,
转移(赋值,复制)给TabBarController的navigationItem,
从而显示在导航栏上,
因为TabBarController就是导航控制器的根控制器,也同时就是栈顶控制器,导航控制器只知道它的存在
//
// BeyondTabBarController.m
// 25_彩票
//
// Created by beyond on 14-8-27.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//
#import "BeyondTabBarController.h"
#import "BeyondTabBar.h"
#import "BeyondTabBarItem.h"
#import "BeyondTabBarDelegate.h"
@interface BeyondTabBarController ()<BeyondTabBarDelegate>
@end
@implementation BeyondTabBarController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 1.釜底抽薪 直接删除默认的tabBar
[self.tabBar removeFromSuperview];
// 2.创建tabbar
BeyondTabBar *myTabBar = [[BeyondTabBar alloc] init];
// 占位原来的tabBar
myTabBar.frame = self.tabBar.frame;
// 代理设置后,可以接收tabBar内部按钮的点击状态切换
myTabBar.delegate = self;
// 添加到当前控制器的view
[self.view addSubview:myTabBar];
// 3.由于 图片名的规律性,一次性添加5个tabBarItem按钮
for (int i = 1; i<=5; i++) {
NSString *normal = [NSString stringWithFormat:@"TabBar%d", i];
NSString *selected = [normal stringByAppendingString:@"Sel"];
// 调用tabBar开放出来的接口,向tabBar内部添加按钮,只要传参:图片名
[myTabBar addOneTabBarItem:normal selectedIconName:selected];
}
});
}
#pragma mark - tabbar代理方法
- (void)tabBar:(BeyondTabBarItem *)tabBar didSelectButtonFrom:(NSUInteger)from to:(NSUInteger)to
{
// 1.直接通过索引 选中某个控制器(这个是UITabBarController自带的API)
self.selectedIndex = to;
UITableViewController *newVC = self.childViewControllers[to];
// 2.将tabBar上的对应按钮的子控制器的navigationItem值转移给TabBarController,因为导航控制器的根控制器就是TabBarController,导航控制器 只知道它的存在
[self.navigationItem copyFromItem:newVC.navigationItem];
}@end
导航栏的适配
只需提供64高和44高的背景图片即可
//
// BeyondNavigationController.m
// 25_彩票
//
// Created by beyond on 14-8-27.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//
#import "BeyondNavigationController.h"
@interface BeyondNavigationController ()
@end
@implementation BeyondNavigationController
#pragma mark 一个类只会调用一次
+ (void)initialize
{
// 1.取出设置主题的对象
UINavigationBar *navBar = [UINavigationBar appearance];
UIBarButtonItem *barItem = [UIBarButtonItem appearance];
// 2.设置导航栏的背景图片
NSString *navBarBg = nil;
// 判断iOS7
// [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0
if (iOS7) {
// 使用64高度的图片,做导航栏背景图片
navBarBg = @"NavBar64";
// 设置导航栏的渐变色为白色(iOS7中返回箭头的颜色变为这个颜色:白色)
navBar.tintColor = [UIColor whiteColor];
} else {
// 非iOS7,使用44高度的图片
navBarBg = @"NavBar";
// 黑色的顶部状态条
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
// 设置导航栏按钮的背景图片
[barItem setBgImgForNormal:@"NavButton" highlighted:@"NavButtonPressed"];
// 设置导航栏返回按钮的背景图片
[barItem setBackBtnBgImgForNormal:@"NavBackButton" highlighted:@"NavBackButtonPressed"];
}
[navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];
// 3.设置导航栏标题颜色为白色
[navBar setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
// 4.设置导航栏按钮文字颜色为白色
[barItem setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:13]
} forState:UIControlStateNormal];
}
#pragma mark 控制状态栏的样式
/*
状态栏的管理:
1> iOS7之前:UIApplication
2> iOS7开始:交给对应的控制器去管理
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
// 白色样式
return UIStatusBarStyleLightContent;
}
@end