前言
現在很多app,尤其一些直播的app,都在做如上的tabBar點擊效果,點擊兩邊的按鈕是蘋果默認的切換方式,點擊中間按鈕,是模態出一個新控制器的view,這里主要涉及到自定義UITabBarController,你可能還看到modal出的新控制器的view是半透明的,在顯示自己view的同時也可以看到原先控制器的視圖,這里主要用了控制器的一個modalPresentationStyle屬性來設置,最近看到很多直播app的如上效果,于是想著自己封裝實現一下,放出來,供大家參考。
正文
自定義UITabBarController:
自定義UITabBarController的實現原理其實很簡單,就是隱藏蘋果自帶的tabBar,使用自定義UIView代替;然后自定義Button加在自定義的tabBar的view上;最后將自定義的button 與UITabBarController的子viewController一一對應。而自定義的button需要使上面顯示按鈕圖片,下面是文字Label,實現這些的重點是計算Button的imageView和Label的寬度和X坐標值。
下面是具體的實現代碼:
CustomTabBar:繼承自UIView,充當tabBar
#import <UIKit/UIKit.h>
@class CustomTabBar;
@protocol CustomTabBarDelegate <NSObject>
- (void)myTabbar:(CustomTabBar *)tabbar btnClicked:(NSInteger)index;
@end
@interface CustomTabBar : UIView
/** 初始化方法
參數1: 位置大小
參數2: 內部按鈕個數
*/
- (instancetype)initWithFrame:(CGRect)frame itemCount:(NSInteger)itemCount;
@property (nonatomic, weak) id<CustomTabBarDelegate> delegate;
@end
#import "CustomTabBar.h"
#import "CustomTabBarItemButton.h"
@interface CustomTabBar ()
@property (nonatomic,assign) NSInteger itemCount;// 子控制器個數
@property (nonatomic,assign) UIButton *previousBtn;// 前一個被點擊的按鈕
@end
@implementation CustomTabBar
- (instancetype)initWithFrame:(CGRect)frame itemCount:(NSInteger)itemCount
{
if (self = [super initWithFrame:frame]) {
// 保存按鈕個數
_itemCount = itemCount;
// 設置背景顏色
self.backgroundColor = [UIColor whiteColor];
// 創建按鈕
[self createBtn];
}
return self;
}
#pragma mark 創建tabBarItem
- (void)createBtn {
// 循環創建按鈕
// 計算按鈕的寬高
CGFloat w = self.bounds.size.width / (self.itemCount+1);
CGFloat h = self.bounds.size.height;
NSArray *selectedImgArr = @[@"tab_live_p",@"tab_launch",@"tab_me_p"];
NSArray *normalImgarr = @[@"tab_live",@"tab_launch",@"tab_me"];
NSArray *titleArr = @[@"首頁",@"",@"我"];
for (int i = 0; i < self.itemCount+1; i ++) {
if(0 == i){
CustomTabBarItemButton *btn = [[CustomTabBarItemButton alloc] initWithFrame:CGRectMake(i * w, 0, w, h) imgName:normalImgarr[i] selectedImgName:selectedImgArr[i] titleColor:[UIColor colorWithRed:34/255.0 green:209/255.0 blue:188/255.0 alpha:1.0] title:titleArr[i]];
btn.tag = 0;
// 添加按鈕的點擊事件
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[self btnClicked:btn];
}
if (1 == i) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake((self.frame.size.width-87)/2, -37, 87, 87);
[btn setImage:[UIImage imageNamed:selectedImgArr[i]] forState:UIControlStateNormal];
btn.tag = 2;
// 添加按鈕的點擊事件
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
if(2 == i){
CustomTabBarItemButton *btn = [[CustomTabBarItemButton alloc] initWithFrame:CGRectMake(i * w, 0, w, h) imgName:normalImgarr[i] selectedImgName:selectedImgArr[i] titleColor:[UIColor colorWithRed:34/255.0 green:209/255.0 blue:188/255.0 alpha:1.0] title:titleArr[i]];
btn.tag = 1;
// 添加按鈕的點擊事件
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
}
#pragma mark tabBarItem被點擊讓代理使UITabBarController的子控制器與其對應
- (void)btnClicked:(UIButton *)btn {
if (0 == btn.tag||1 == btn.tag) {
_previousBtn.selected = NO;
btn.selected = YES;
_previousBtn = btn;
}
if ([_delegate respondsToSelector:@selector(myTabbar:btnClicked:)]) {
[_delegate myTabbar:self btnClicked:btn.tag];
}
}
#pragma mark 當按鈕超過了父視圖范圍,點擊是沒有反應的。因為消息的傳遞是從最下層的父視圖開始調用hittest方法。當存在view時才會傳遞對應的event,現在點擊了父視圖以外的范圍,自然返回的是nil。所以當子視圖(比如按鈕btn)因為一些原因超出了父視圖范圍,就要重寫hittest方法,讓其返回對應的子視圖,來接收事件。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
UIButton *btn = (UIButton *)[self viewWithTag:2];
CGPoint tempoint = [btn convertPoint:point fromView:self];
//判斷給定的點是否被一個CGRect包含,可以用CGRectContainsPoint函數
if (CGRectContainsPoint(btn.bounds, tempoint))
{
view = btn;
}
}
return view;
}
CustomTabBarItemButton:繼承自UIButton,充當tabBarItem
#import <UIKit/UIKit.h>
@interface CustomTabBarItemButton : UIButton
- (instancetype)initWithFrame:(CGRect)frame imgName:(NSString *)imgName selectedImgName:(NSString *)selectedImgName titleColor:(UIColor *)color title:(NSString *)title;
@end
#import "CustomTabBarItemButton.h"
@implementation CustomTabBarItemButton
- (instancetype)initWithFrame:(CGRect)frame imgName:(NSString *)imgName selectedImgName:(NSString *)selectedImgName titleColor:(UIColor *)color title:(NSString *)title
{
if (self = [super initWithFrame:frame]) {
// 設置文字,字號,圖片,文字顏色
[self setTitle:title forState:UIControlStateNormal];
self.titleLabel.font = [UIFont systemFontOfSize:12];
[self setTitleColor:color forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:selectedImgName] forState:UIControlStateSelected];
// 設置文字和圖片的位置
self.imageView.contentMode = UIViewContentModeCenter;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
}
#pragma mark 設置高亮狀態的方法
// 在這個方法里面,系統會默認給按鈕設置高亮狀態的的情景
// 覆蓋此方法,會使按鈕的高亮狀態不呈現任何情景
- (void)setHighlighted:(BOOL)highlighted {}
#pragma mark 設置文字frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
// 標簽視圖高度占按鈕高度的1/3
return CGRectMake(0, self.bounds.size.height / 3 * 2, self.bounds.size.width, self.bounds.size.height / 3);
}
#pragma mark 設置圖片frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
// 圖片視圖高度占按鈕高度的2/3
return CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height / 3 * 2);
}
@end
MyTabBarViewController:繼承自UITabBarController
#import "MyTabBarViewController.h"
#import "CustomTabBar.h"
#import "HomeViewController.h"
#import "LiveViewController.h"
#import "MYCenterViewController.h"
#import "AppDelegate.h"
@interface MyTabBarViewController ()<CustomTabBarDelegate>
@end
@implementation MyTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.設置所有子控制器
[self addChildViewControllers];
// 2.創建自定義的tabbar
[self createCustomTabbar];
}
#pragma mark 設置所有的子控制器
- (void)addChildViewControllers {
HomeViewController *homeVC = [[HomeViewController alloc] init];
MYCenterViewController *myCenterVC = [[MYCenterViewController alloc] init];
// 設置子控制器
self.viewControllers = @[homeVC, myCenterVC];
}
#pragma mark 隱藏系統自帶的tabBar,創建自定義的tabBar
- (void)createCustomTabbar {
// 1.隱藏系統tabbar
self.tabBar.hidden = YES;
// 2.創建自定義的tabbar
CustomTabBar *myTabbar = [[CustomTabBar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) itemCount:self.viewControllers.count];
myTabbar.delegate = self;
[self.view addSubview:myTabbar];
}
#pragma mark CustomTabBarDelegate方法,tabBarItem被點擊
- (void)myTabbar:(CustomTabBar *)tabbar btnClicked:(NSInteger)index
{
if (2 == index) {
/*
*當點擊中間的按鈕,modal出LiveViewController的視圖,并且不覆蓋原先視圖
*/
//iOS 8.0之后的方法
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0){
LiveViewController *liveVC = [[LiveViewController alloc] init];
//設置modal出的視圖view的透明度
liveVC.view.alpha = 0.9;
//此模式必須設置,否則會覆蓋原先視圖,看不到原先視圖的view
liveVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:liveVC animated:YES completion:nil];
}else{
//在iOS7或更低需要設置你的window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext
AppDelegate *appdelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
LiveViewController *liveVC = [[LiveViewController alloc] init];
liveVC.view.alpha = 0.9;
appdelegate.window.rootViewController.modalPresentationStyle=UIModalPresentationCurrentContext;
[appdelegate.window.rootViewController presentViewController:liveVC animated:YES completion:nil];
}
}else{
//設置選擇的子控制器與點擊的按鈕相對應
self.selectedIndex = index;
}
}
@end
modal出的新控制器的半透明的view:
模態出一個半透明的視圖, 在目標視圖中設置背景顏色然后發現模態動作結束后變成了黑色或者不是半透明的顏色。在iOS8之后只需要為要present的控制器的modalPresentationStyle屬性設置一個最新的值UIModalPresentationOverCurrentContext就可以解決這種需求。
然而這個屬性是iOS 8才出來的,所以針對iOS 7或更低的系統,需要設置window的根控制器的modalPresentationStyle為UIModalPresentationCurrentContext。
代碼如下:
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0){
LiveViewController *liveVC = [[LiveViewController alloc] init];
//設置modal出的視圖view的透明度
liveVC.view.alpha = 0.9;
//此模式必須設置,否則會覆蓋原先視圖,看不到原先視圖的view
liveVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:liveVC animated:YES completion:nil];
}else{
//在iOS7或更低需要設置你的window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext
AppDelegate *appdelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
LiveViewController *liveVC = [[LiveViewController alloc] init];
liveVC.view.alpha = 0.9;
appdelegate.window.rootViewController.modalPresentationStyle=UIModalPresentationCurrentContext;
[appdelegate.window.rootViewController presentViewController:liveVC animated:YES completion:nil];
}
源碼已上傳至fenglinyunshi-git,歡迎下載,并提出寶貴意見。