簡介
UITabBarController - 選項卡控制器,與導航控制器一樣,也被廣泛用于各種ios應用程序。顧名思義,選項卡控制器在屏幕底部顯示一系列“選顯卡”,這些選項卡表示為圖標和文本,用戶觸摸它們將在不同的場景間切換。和UINavigationController類似,UITabBarController也可以用來控制多個頁面導航,用戶可以在多個視圖控制器之間移動,并可以定制屏幕底部的選項卡欄。
借助屏幕底部的選項卡欄,UITabBarController不必像UINavigationController那樣以棧的方式推入和推出視圖,而是建立一系列的控制器(這些控制器可以是UIViewController、UINavigationController、UITableViewController等)并將它們添加到選項卡欄,使每個選項卡對應一個控制器。每個場景都呈現了應用程序的一項功能,或是提供了一種查看應用程序的獨特方式。UITabBarController是iOS中很常用的一個viewController,例如系統的鬧鐘程序等,QQ也是用的UITabBarController。UITabBarController通常作為整個程序的rootViewController,而且不能添加到別的container viewController中。
與導航控制器一樣,選項卡控制器會為我們處理一切。當用戶觸摸按鈕時會在場景之間進行切換,我們無需以編程的方式處理選項卡欄事件,也無需手工在視圖控制器之間切換。
UITabBarController的使用步驟
- 初始化UITabBarController控制器
- 設置UIwindow的rootViewController為這個UITabBarController
- 在UITabBarController中添加子控制器
示例代碼
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 創建窗口
self.window = [[UIWindow alloc]init];
self.window.frame = [UIScreen mainScreen].bounds;
// 設置窗口的跟控制器
UITabBarController * tabbarVC = [[UITabBarController alloc]init];
// 添加子控制器
UIViewController * VC01 = [[UIViewController alloc]init];
// 設置標題
VC01.tabBarItem.title = @"精華";
// 設置默認圖片
VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
// 設置選中圖片
VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
VC01.view.backgroundColor = [UIColor yellowColor];
[tabbarVC addChildViewController:VC01];
UIViewController * VC02 = [[UIViewController alloc]init];
VC02.tabBarItem.title = @"新帖";
VC02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
VC02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
VC02.view.backgroundColor = [UIColor redColor];
[tabbarVC addChildViewController:VC02];
UIViewController * VC03 = [[UIViewController alloc]init];
VC03.tabBarItem.title = @"關注";
VC03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
VC03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
VC03.view.backgroundColor = [UIColor blueColor];
[tabbarVC addChildViewController:VC03];
UIViewController * VC04 = [[UIViewController alloc]init];
VC04.tabBarItem.title = @"我";
VC04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
VC04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
VC04.view.backgroundColor = [UIColor greenColor];
[tabbarVC addChildViewController:VC04];
self.window.rootViewController = tabbarVC;
// 顯示窗口
[self.window makeKeyAndVisible];
return YES;
}
但是這樣在appdelegate里面進行設置會有很多缺點:
- 底部tabbaritem都是默認的藍色
- 添加子控制器的代碼暴露在了外面
以自定義的方式實現UITabBarController
步驟:
- 新建一個繼承自UITabBarController的子類
- 將這個子類設置為根控制器
- 在這個子類里面實現添加子控制器的方法
但是在這種情況下因為有系統默認的藍色渲染,會導致選中圖標selectedImage不是圖片原來的樣子,可以通過以下兩種方式解決:
- 方法1:設置選中圖片的渲染模式
// 添加子控制器
UIViewController * VC01 = [[UIViewController alloc]init];
// 設置標題
VC01.tabBarItem.title = @"精華";
// 設置默認圖片
VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
// 設置渲染模式 - 保持原始的渲染
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 設置選中圖片
VC01.tabBarItem.selectedImage =image;
VC01.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:VC01]
-
方法2:直接在圖片文件夾的屬性里面進行設置 - 將render as設置成original image
由于文字和圖片一樣是默認的藍色,所以也要進行設置才能達到自己想要的效果
方案一:通過setTitleTextAttributes屬性設置(但是這個方法很麻煩,每次設置的時候都需要寫很多代碼)
- setTitleTextAttributes設置的時候是通過字典設置的,所以先搞一個字典
// 添加子控制器
UIViewController * VC01 = [[UIViewController alloc]init];
// 設置標題
VC01.tabBarItem.title = @"精華";
// 設置默認圖片
VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
//設置選中圖片
VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
// 設置文字屬性
NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0];
// 設置文字的前景色
attrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
[VC01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
VC01.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:VC01];
優化方案(appearance)
- 由于文字屬性每次設置的時候代碼很多很煩,所以要進行簡化
- 當方法方法后面有UI_APPERANCE宏的時候,就可以通過appearance對象一次性設置
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 步驟:
- 拿到那個item的apperance
- 對這個item進行設置、
- 之后所有的item都會是這個屬性
- (void)viewDidLoad {
[super viewDidLoad];
// 通過appearance統一設置UITabbarItem的文字屬性
NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0]; // 設置文字大小
attrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 設置文字的前景色
NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
UITabBarItem * item = [UITabBarItem appearance]; // 設置appearance
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
// 添加子控制器
UIViewController * VC01 = [[UIViewController alloc]init];
// 設置標題
VC01.tabBarItem.title = @"精華";
// 設置默認圖片
VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
//設置選中圖片
VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
[VC01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
VC01.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:VC01];
UIViewController * VC02 = [[UIViewController alloc]init];
VC02.tabBarItem.title = @"新帖";
VC02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
VC02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
VC02.view.backgroundColor = [UIColor redColor];
[self addChildViewController:VC02];
UIViewController * VC03 = [[UIViewController alloc]init];
VC03.tabBarItem.title = @"關注";
VC03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
VC03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
VC03.view.backgroundColor = [UIColor blueColor];
[self addChildViewController:VC03];
UIViewController * VC04 = [[UIViewController alloc]init];
VC04.tabBarItem.title = @"我";
VC04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
VC04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
VC04.view.backgroundColor = [UIColor greenColor];
[self addChildViewController:VC04];
}
封裝 - 封裝添加自定義子控制器的方法
- 由于自定義子控制器的代碼很相似,所以可以進行抽取
- (void)viewDidLoad {
[super viewDidLoad];
// 通過appearance統一設置UITabbarItem的文字屬性
NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0]; // 設置文字大小
attrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 設置文字的前景色
NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
UITabBarItem * item = [UITabBarItem appearance]; // 設置appearance
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
// 添加子控制器
[self setupChildVC:@"精華" andImage:@"tabBar_essence_icon" andSelectImage:@"tabBar_essence_click_icon"];
[self setupChildVC:@"新帖" andImage:@"tabBar_new_icon" andSelectImage:@"tabBar_new_click_icon"];
[self setupChildVC:@"關注" andImage:@"tabBar_friendTrends_icon" andSelectImage:@"tabBar_friendTrends_click_icon"];
[self setupChildVC:@"我" andImage:@"tabBar_me_icon" andSelectImage:@"tabBar_me_click_icon"];
}
/**
* 初始化子控制器
*/
- (void)setupChildVC:(NSString * )title andImage:(NSString * )image andSelectImage:(NSString *)selectImage{
UIViewController * VC = [[UIViewController alloc]init];
VC.tabBarItem.title = title;
VC.tabBarItem.image = [UIImage imageNamed:image];
VC.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
VC.view.backgroundColor = [UIColor greenColor];
[self addChildViewController:VC];
}
自定義底部tabbar樣式
如果想要在這個tabbar上面添加一個按鈕(注意不是item,tabbaritem是圖片在上,文字在下的格式),這個按鈕和item樣式不一樣,占據了和item上文字加上圖片的大小。由于添加到tabbar上面的item是從左到右順序排列的,如果是直接添加一個item,那么不能達到我們想要的樣式(當然,如果你是添加一個item的話,直接按照以前的方法就行了),這個時候可以通過自定義tabbar來實現。
- 由于tabbar是read-only屬性,所以只能通過KVC模式跟換tabbar
步驟:
- 新建一個繼承自UITabbar的類
- 在這個類里面實現初始化
- 在
layoutSubviews
方法里面重新布局 - 在TabbarController里面跟換tabbar
- 雖然我們在TabbarController里面換成了自定義的tabbar,但是因為這個tabbar繼承自uitabbar,所以它原來的屬性和內容還在
#import "LMHTabbar.h"
@interface LMHTabbar()
/** 發布按鈕 */
@property (nonatomic, weak) UIButton * publishBtn;
@end
@implementation LMHTabbar
/**
* 初始化
*/
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// 設置tabbar的子控件
UIButton * publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[publishBtn sizeToFit];
[self addSubview:publishBtn];
self.publishBtn = publishBtn;
}
return self;
}
/**
* 重寫布局子控件的方法進行布局
*/
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
// 設置發布按鈕的frame
self.publishBtn.center = CGPointMake(width * 0.5, height * 0.5);
// 設置其他的UITabBar的frame
CGFloat buttonY = 0;
CGFloat buttonW = width /5;
CGFloat buttonH = height;
NSInteger index = 0;
for (UIView * button in self.subviews) {
// 判斷 - 只有是UITabBarButton的button才進行布局(也就是tabbar上面的精華、新帖、關注、我這四個按鈕) 而發布按鈕不是UITabBarButton,就不進行布局
// 由于UITabBar是蘋果官方私有的, 所以不能直接設置
if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
// 計算按鈕的x值
CGFloat buttonX = buttonW * ((index > 1) ? (index + 1): (index));
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
// 索引增加
index ++;
}
}
@end