前言
现在很多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,欢迎下载,并提出宝贵意见。