http://www.reibang.com/p/bd95c56b3ff9
1.窗口的根控制器為TabBar控制器
實現(xiàn)
appDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1刘莹、創(chuàng)建窗口
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的內(nèi)容
-(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中統(tǒng)一設置navBar的內(nèi)容
//當 當前類第一次初始化的時候調(diào)用竭恬,但是如果有子類社裆,會調(diào)用多次
+(void)initialize{
//如果是在當前類翁授,則執(zhí)行以下設置
if(self == [PDNavigationViewController class]){
//1.獲取APP的導航條標識
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[PDNavigationViewController class],nil]; //獲取某幾個類的導航條標識
//2.通過標識來設置內(nèi)容
//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.創(chuàng)建窗口
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//2.創(chuàng)建導航控制器
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中,創(chuàng)建子控制器涎拉,并設置子控制器的TabBarItem 的內(nèi)容
- (void)viewDidLoad {
[super viewDidLoad];
//1.創(chuàng)建所有子控制器
//創(chuàng)建子控制器1
PDnearbyViewController *nearby = [[PDnearbyViewController alloc]init];
//設置子控制器的TabBarItem的內(nèi)容
nearby.tabBarItem.title = @"附近的人";
nearby.tabBarItem.selectedImage = [UIImage imageNamed:@"nearby"];
nearby.tabBarItem.image = [UIImage imageNamed:@"nearby"];
//創(chuàng)建子控制器2
AHDynamicViewController *dynamic = [[AHDynamicViewController alloc] init];
dynamic.tabBarItem.title = @"動態(tài)";
dynamic.tabBarItem.selectedImage = [UIImage imageNamed:@"dynamic"];
dynamic.tabBarItem.image = [UIImage imageNamed:@"dynamic"];
//創(chuàng)建子控制器3
AHAddViewController *add = [[AHAddViewController alloc] init];
add.tabBarItem.title = @"添加";
add.tabBarItem.selectedImage = [UIImage imageNamed:@"activity"];
add.tabBarItem.image = [UIImage imageNamed:@"activity"];
//創(chuàng)建子控制器4
瑞侮。
。同上
鼓拧。
//2.把創(chuàng)建的所有子控制器 添加到TabBar控制器的子控制器數(shù)組中
self.viewControllers = @[nearby,
dynamic,
add,
message,
my
];
self.title = collectionVC.tabBarItem.title; //初始時導航控制器的標題
self.delegate = self;
}
另外半火,導航控制器的TabBar內(nèi)容在它的.m中統(tǒng)一設置
。季俩。钮糖。