核心思路就是在自定义的TabBarController里面用kvc替换系统的TabBar为自定义的MyTabBar,然后在MyTabBar里面留出位置放置一个我们想要的圆形的控件。
git源码:https://github.com/KingSering/YYTabBar
MyTabBar.h文件
//-------------------------------------------------------------------------------------
#import<UIKit/UIKit.h>
@protocol RoundButtonDelegate <NSObject>
-(void)RoundButtonClicked;
@end
@interface MyUITabBar : UITabBar
@property(nonatomic,weak)id <RoundButtonDelegate>myDelegate;
@property(nonatomic,strong)UIButton*roundButton;
@end
//-------------------------------------------------------------------------------------
MyTabBar.m文件
//-------------------------------------------------------------------------------------
#import "MyUITabBar.h"
@implementation MyUITabBar
//懒加载
-(UIButton*)roundButton
{
if (!_roundButton)
{
_roundButton=[[UIButtonalloc]init];
}
return _roundButton;
}
-(void)roundButtonClicked
{
if([self.myDelegaterespondsToSelector:@selector(RoundButtonClicked)])
{
[self.myDelegate RoundButtonClicked];
}
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.roundButton.backgroundColor=[UIColor whiteColor];
[self.roundButton setBackgroundImage:[UIImage imageNamed:@"yuanqubao.png"] forState:UIControlStateNormal];
[self.roundButtonaddTarget:selfaction:@selector(roundButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.roundButton];
intcenterx =self.bounds.size.width*0.5;
intcentery =self.bounds.size.height*0.5;
self.roundButton.frame=CGRectMake(centerx-30, centery-50,60,60);
self.roundButton.layer.cornerRadius=30;
self.roundButton.clipsToBounds=YES;
Class class = NSClassFromString(@"UITabBarButton");
intindex =0;
inttabWidth =self.bounds.size.width/3.0;
for(UIView*viewinself.subviews)
{
//找到UITabBarButton 类型的子控件
if([viewisKindOfClass:class])
{
CGRectrect = view.frame;
rect.origin.x= index*tabWidth;
rect.size.width= tabWidth;
view.frame= rect;
index++;
//留出位置放置中间凸出按钮
if(index==1) {
index++;
}
}
}
}
//响应触摸事件,如果触摸位置位于圆形按钮控件上,则由圆形按钮处理触摸消息
- (UIView*)hitTest:(CGPoint)pointwithEvent:(UIEvent*)event{
//判断tabbar是否隐藏
if(self.hidden==NO) {
if ([self touchPointInsideCircle:self.roundButton.center radius:30 targetPoint:point]) {
//如果位于圆形按钮上,则由圆形按钮处理触摸消息
returnself.roundButton;
}
else{
//否则系统默认处理
return[superhitTest:pointwithEvent:event];
}
}
return[superhitTest:pointwithEvent:event];
}
- (BOOL)touchPointInsideCircle:(CGPoint)center radius:(CGFloat)radius targetPoint:(CGPoint)point
{
CGFloatdist =sqrtf((point.x- center.x) * (point.x- center.x) +
(point.y- center.y) * (point.y- center.y));
return(dist <= radius);
}
@end
//-------------------------------------------------------------------------------------
BaseTabBarController.m文件
//-------------------------------------------------------------------------------------
#import "BaseTabBarController.h"
#import "MyUITabBar.h"
#import "ViewController.h"
@interface BaseTabBarController ()<RoundButtonDelegate>
@end
@implementation BaseTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
//左右两边的控制器
[self setTabBarName];
//中间凸起的控制器
ViewController*vc=[[ViewController alloc]init];
BaseNavigationController*navi=[[BaseNavigationController alloc]initWithRootViewController:vc];
[self addChildViewController:navi];
//替换系统的TabBar
MyUITabBar*tabbar = [[MyUITabBaralloc]init];
tabbar.myDelegate=self;
//修改系统的Tabbar,使用我们自定义的Tabbar
[selfsetValue:tabbarforKeyPath:@"tabBar"];
// Do any additional setup after loading the view.
}
-(void)RoundButtonClicked
{
// ViewController*vc=[[ViewController alloc]init];
// vc.view.backgroundColor=[UIColor yellowColor];
// BaseNavigationController*navi =[[BaseNavigationController alloc] initWithRootViewController:vc];
// [self presentViewController:navi animated:YES completion:nil];
[self viewControllers][0].tabBarController.selectedIndex = 2;
}
#pragma mark- 创建tabBar
- (void)setTabBarName
{
//主页
HomeViewController *vc1 = [[HomeViewController alloc] init];
BaseNavigationController*nav1=(BaseNavigationController*) [self addChildVc:vc1 title:LocalizedString(@"首页") image:@"shouye_gray" selectedImage:@"shouye_cheng+1"];
//我的
PersonViewController *vc2 = [[PersonViewController alloc] initWithNibName:@"PersonViewController" bundle:[NSBundle mainBundle]];
BaseNavigationController*nav2=(BaseNavigationController*) [self addChildVc:vc2 title:LocalizedString(@"我的") image:@"my_gray" selectedImage:@"my_cheng+1"];
NSArray*vsArr =@[nav1,nav2];
for (BaseNavigationController*vv in vsArr)
{
[self addChildViewController:vv];
}
}
/**
* 添加一个子控制器
*
* @param childVc 子控制器
* @param title 标题
* @param image 图片
* @param selectedImage 选中的图片
*/
- (id)addChildVc:(UIViewController*)childVctitle:(NSString*)titleimage:(NSString*)imageselectedImage:(NSString*)selectedImage
{
UIImage*normalImage = [[selfscaleToSize:[UIImageimageNamed:image]size:CGSizeMake(30,30)]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage*select_img= [ [selfscaleToSize:[UIImageimageNamed:selectedImage]size:CGSizeMake(30,30)]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVc.tabBarItem.image= normalImage;
childVc.tabBarItem.selectedImage=select_img;
// 设置子控制器的文字
childVc.tabBarItem.title= title;// 设置tabbar的文字
// 设置文字的样式
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
//RGBA(255, 157, 38, 1)
selectTextAttrs[NSForegroundColorAttributeName] = MAINCOLOR;
[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVc.tabBarItemsetTitleTextAttributes:selectTextAttrsforState:UIControlStateSelected];
// 先给外面传进来的小控制器 包装 一个导航控制器
BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:childVc];
returnnav;
}
//iOS上直接缩小UIImageView的大小会产生锯齿,可以先将其缩放后再使用.
- (UIImage*)scaleToSize:(UIImage*)imgsize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[imgdrawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnscaledImage;
}
@end
//-------------------------------------------------------------------------------------
网友评论