一、UITabBarController
微信底部那一排 微信跛锌、通訊錄、發(fā)現(xiàn)届惋、朋友圈髓帽,就是UITabBarController
它上面的控制的四個(gè)視圖控制器是平級(jí)的,一般情況下將self.window.rootViewController設(shè)置為UITabBarController
然后在UITabBarController上面添加UINavigationController
UINavigationController上面添加一般的UIViewController
UITabBarController-->UINavigationController-->UIViewController
二脑豹、屬性和方法
UITabBar的高度是49郑藏,最多顯示5個(gè)UITabBarItem,超過(guò)5個(gè)會(huì)增加一個(gè)更多按鈕瘩欺。
#import "RootTabBarController.h"
#import "SecondViewController.h"
//創(chuàng)建一個(gè)ViewController加到NavigationController上
//再將NavigationController加到TabBarController上
RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//創(chuàng)建UITabBarController
UITabBarController *myTabBar = [UITabBarController new];
//將導(dǎo)航控制器放入tabBar
myTabBar.viewControllers = @[navigationC];
//設(shè)置底部的item標(biāo)題译秦,這個(gè)字很小
navigationC.tabBarItem.title = @"根";
//設(shè)置圖片,這里圖片大小不能自適應(yīng)击碗,最好把圖片裁好 40*40筑悴,照片還有設(shè)置渲染效果,背景色會(huì)影響照片顯示
navigationC.tabBarItem.image = [[UIImage imageNamed:@"b3.gif"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
//設(shè)置tabBar的字體顏色
myTabBar.tabBar.tintColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
//設(shè)置tabbar背景顏色
myTabBar.tabBar.barTintColor = [UIColor colorWithRed:200/255.0 green:233/255.0 blue:160/255.0 alpha:1];
//是否半透明
myTabBar.tabBar.translucent = NO;
//將tabBarController設(shè)置為根視圖控制器
self.window.rootViewController = myTabBar;
//把SecondView添加
SecondViewController *secondVC = [[SecondViewController alloc]init];
UINavigationController *secondNavC = [[UINavigationController alloc]initWithRootViewController:secondVC];
//設(shè)置標(biāo)題
secondNavC.tabBarItem.title = @"second";
//設(shè)置item角標(biāo)稍途,右上角的紅色數(shù)字阁吝,未讀消息數(shù)
secondNavC.tabBarItem.badgeValue = @"10";
//當(dāng)前被選中的頁(yè)面/按鈕
myTabBar.selectedIndex = 0;
//將第三個(gè)界面加入
tabbar ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
UINavigationController *thirdNavC = [[UINavigationController alloc]initWithRootViewController:thirdVC];
thirdNavC.tabBarItem.title = @"third";
//將所有視圖加入
tabbar myTabBar.viewControllers = @[navigationC,secondNavC,thirdNavC];
//設(shè)置默認(rèn)頁(yè)
myTabBar.selectedIndex = 1;
//設(shè)置為根視圖控制器 RootTabBarViewController *rootTabBarVC = [[RootTabBarViewController alloc]init];
self.window.rootViewController = rootTabBarVC
UITabBarController 自帶邏輯,點(diǎn)擊下面的分區(qū)bar會(huì)自動(dòng)跳轉(zhuǎn)到相應(yīng)的NavigationController下的ViewController
這是UITabBarItem的樣式和對(duì)應(yīng)的枚舉值
三械拍、自定義UITabBarViewController
系統(tǒng)的TabBar有時(shí)不能滿足我們的需求突勇,我們可以更改它的外觀。
自定義一個(gè)UIView的視圖坷虑,貼在UITabBar的View上
將UITabBarController的tabbar屬性的hidden設(shè)置為YES甲馋,就可以隱藏原有的tabbar顯示了。
先寫(xiě)自定義的tabBarUIview迄损,思路是做一個(gè)底部橫條視圖定躏,給上面加上自定義的button,給button加上方法芹敌,該方法來(lái)改變系統(tǒng)的UITabBarController的selectedIndex值痊远。
這里涉及一個(gè)頁(yè)面間傳值的問(wèn)題,可以使用block或者協(xié)議方法
customTabBarView.h
#import <UIKit/UIKit.h>
//block傳值
typedef void(^passValue)(NSInteger tag);
@protocol CustomTabBarDelegate <NSObject>
//把btn的tag傳出去的方法
-(void)selectedIndexWithTag:(NSInteger)tag;
@end
@interface CustomTabBarView : UIView
@property (nonatomic,copy)passValue passValueTag;
@property (nonatomic,assign)id <CustomTabBarDelegate>delegate;
@end
customTabBarView.m
#import "CustomTabBarView.h"
@implementation CustomTabBarView
//創(chuàng)建按鈕
-(void)createSubViews
{
for (int i = 0; i < 2; i ++) {
//初始化按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];
//點(diǎn)擊時(shí)的樣式
[btn setTitle:@"選" forState:UIControlStateHighlighted];
[btn setTitle:@"33" forState:UIControlStateFocused];
[btn setImage:[[UIImage imageNamed:@"b33"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
[btn setTitle:@"" forState:UIControlStateSelected];
//選中狀態(tài)
// btn.selected = NO;
//通過(guò)tag值來(lái)確定我們點(diǎn)擊的是哪一個(gè)按鈕氏捞,來(lái)切換到對(duì)應(yīng)的視圖控制器
[btn setTag:1000+i];
//設(shè)置點(diǎn)擊方法
[btn addTarget:self action:@selector(customSelectedIndex:) forControlEvents:UIControlEventTouchDown];
//設(shè)置frame
btn.frame = CGRectMake((414)/2*i, 0, 414/2, 50);
[self addSubview:btn];
}
}
//按鈕的回調(diào)方法
-(void)customSelectedIndex:(UIButton*)sender
{
for (int i = 0; i< 2; i++) {
UIButton *btn = (UIButton*)[self viewWithTag:1000+i];
btn.selected = NO;
}
sender.selected = YES;
// 判斷在指定的代理類(lèi)中是否實(shí)現(xiàn)了該協(xié)議方法
// 確保執(zhí)行時(shí)無(wú)此方法時(shí)不崩潰
if([self.delegate respondsToSelector:@selector(selectedIndexWithTag:)])
{
[self.delegate selectedIndexWithTag:(sender.tag - 1000)];
}
else
{
NSLog(@"selectIndexWithTag該方法沒(méi)有實(shí)現(xiàn)");
}
//調(diào)用block方法
//self.passValueTag(sender.tag - 1000);
}
//初始化自定義的UIView
-(instancetype)initWithFrame:(CGRect)frame
{
//設(shè)置frame
frame = CGRectMake(0, 736-50, 414, 50);
self = [super initWithFrame:frame];
if (self) {
[self createSubViews];
self.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
}
return self;
}
@end
在RootTabBController.m中添加自定義的視圖碧聪,并隱藏原本的TabBar
這里的secondViewController和FirstViewController就是跳轉(zhuǎn)后顯示的VC
#import "RootTabBarController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "CustomTabBarView.h"
//導(dǎo)入?yún)f(xié)議
@interface RootTabBarController ()<CustomTabBarDelegate>
@end
@implementation RootTabBarController
-(void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建視圖控制器
FirstViewController *firstVC = [[FirstViewController alloc]init];
UINavigationController *firstNavC = [[UINavigationController alloc]initWithRootViewController:firstVC];
firstVC.view.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
SecondViewController *secondVC = [[SecondViewController alloc]init];
UINavigationController *secondNavC = [[UINavigationController alloc]initWithRootViewController:secondVC];
secondVC.view.backgroundColor = [UIColor colorWithRed:200/255.0 green:233/255.0 blue:160/255.0 alpha:1];
//給tabBarController添加子控制器
[self addChildViewController:firstNavC];
[self addChildViewController:secondNavC];
//使用自定義的tabbar先把系統(tǒng)的隱藏掉
self.tabBar.hidden = YES;
//初始化自定義的tabbar
CustomTabBarView *customView = [[CustomTabBarView alloc]initWithFrame:CGRectZero];
[self.view addSubview:customView];
//指定代理
customView.delegate = self;
//設(shè)置block內(nèi)容
customView.passValueTag = ^(NSInteger tag)
{
self.selectedIndex = tag;
};
}
//實(shí)現(xiàn)代理方法
- (void)selectedIndexWithTag:(NSInteger)tag
{
self.selectedIndex = tag;
}
@end
做的比較丑和簡(jiǎn)單