效果图

1.在继承自UITabBarController的自定义controller中调用以下方法(LZCustomTabbar为自定义的tabbar)

- (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

[LZCustomTabbar setTabBarUI:self.tabBar.subviews tabBar:self.tabBar topLineColor:[UIColor lightGrayColor] backgroundColor:self.tabBar.barTintColor];

}

2.UITabBarController的.m文件内容如下

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self initUI];

}

- (void)initUI

{

LZCustomTabbar * tabbar = [[LZCustomTabbar alloc] init];

//中间自定义tabBar点击事件

__weak __typeof(self) weakSelf = self;

tabbar.btnClickBlock = ^(UIButton *btn) {

__typeof(&*weakSelf) strongSelf = weakSelf;

strongSelf.selectedIndex = 4;

};

[self setValue:tabbar forKeyPath:@"tabBar"];

//第一个控制器

ViewController * vc1 = [[ViewController alloc] init];

vc1.view.backgroundColor = [UIColor redColor];

UINavigationController * NC1 = [self addChildVc:vc1 title:@"首页" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];

//第2个控制器

UIViewController * vc2 = [[UIViewController alloc] init];

vc2.view.backgroundColor = [UIColor yellowColor];

UINavigationController * NC2 = [self addChildVc:vc2 title:@"课程" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];

//第3个控制器

UIViewController * vc3 = [[UIViewController alloc] init];

vc3.view.backgroundColor = [UIColor blueColor];

UINavigationController * NC3 = [self addChildVc:vc3 title:@"收藏" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];

//第4个控制器

UIViewController * vc4 = [[UIViewController alloc] init];

vc4.view.backgroundColor = [UIColor purpleColor];

UINavigationController * NC4 = [self addChildVc:vc4 title:@"我的" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];

//第5个控制器

UIViewController * vc5 = [[UIViewController alloc] init];

vc5.view.backgroundColor = [UIColor whiteColor];

UINavigationController * NC5 = [[UINavigationController alloc] initWithRootViewController:vc5];

self.viewControllers = @[NC1,NC2,NC3,NC4,NC5];

}

#pragma mark - 添加子控制器  设置图片

/**

*  添加一个子控制器

*

@param childVc       子控制器

@param title         标题

@param image         图片

@param selectedImage 选中的图片

*/

- (UINavigationController*)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage

{

// 设置子控制器的文字

childVc.title = title; // 同时设置tabbar和navigationBar的文字

// 设置子控制器的图片

childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//未选中字体颜色  system为系统字体

[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];

//选中字体颜色

[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateSelected];

// 先给外面传进来的小控制器 包装 一个导航控制器

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];

return nav;

}

#pragma mark - 类的初始化方法,只有第一次使用类的时候会调用一次该方法

+ (void)initialize

{

//tabbar去掉顶部黑线

[[UITabBar appearance] setBackgroundImage:[UIImage new]];

[[UITabBar appearance] setShadowImage:[UIImage new]];

[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];

}

-(void)viewDidLayoutSubviews{

[super viewDidLayoutSubviews];

[LZCustomTabbar setTabBarUI:self.tabBar.subviews tabBar:self.tabBar topLineColor:[UIColor lightGrayColor] backgroundColor:self.tabBar.barTintColor];

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

3.LZCustomTabbar的.m文件内容

@interface LZCustomTabbar ()

@property (nonatomic,weak) UIButton *button;

@end

@implementation LZCustomTabbar

- (UIButton *) button

{

if (_button == nil)

{

UIImage *normalImage = [UIImage originImage:[UIImage imageNamed:@"tabBar_publish_icon"] scaleToSize:CGSizeMake(70, 70)];

UIImage *selectedImage = [UIImage originImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] scaleToSize:CGSizeMake(70, 70)];

UIButton * button = [[UIButton alloc] init];

[button setImage:selectedImage forState:UIControlStateSelected];

[button setImage:normalImage forState:UIControlStateNormal];

[button addTarget:self action:@selector(didBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];

[self addSubview:button];

_button = button;

}

return _button;

}

-(void)didBtnAction:(UIButton*)btn{

if (self.btnClickBlock) {

self.btnClickBlock(btn);

}

}

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

[self setTranslucent:NO];

}

return self;

}

-(void)layoutSubviews

{

[super layoutSubviews];

int index = 0;

CGFloat itemW = self.width/5.0;

for (UIView *subviews in self.subviews)

{

//取到系统tabbar的Item方法

if ([@"UITabBarButton" isEqualToString:NSStringFromClass(subviews.class)])

{

subviews.left = index * itemW;

subviews.width = itemW;

if (index >= 2)

{

subviews.left +=itemW;

}

index++;

}

}

self.button.frame = CGRectMake(0, 0, itemW, itemW);

self.button.center = CGPointMake(self.width/2.0, (self.height - 15 - lz_safeBottomMargin)/2.0);

}

//判断点是否在响应范围

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

if (self.isHidden == NO) {

UIBezierPath *circle  = [UIBezierPath bezierPathWithArcCenter:self.button.center radius:35 startAngle:0 endAngle:2* M_PI clockwise:YES];

UIBezierPath *tabbar  = [UIBezierPath bezierPathWithRect:self.bounds];

if ( [circle containsPoint:point] || [tabbar containsPoint:point]) {

return YES;

}

return NO;

}else {

return [super pointInside:point withEvent:event];

}

}

+(void)setTabBarUI:(NSArray*)views tabBar:(UITabBar*)tabBar topLineColor:(UIColor*)lineColor backgroundColor:(UIColor*)backgroundColor{

[views enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

if ([obj isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {

NSLog(@"++++++++++%@",@"存在");

if ([obj viewWithTag:999] == nil) {

UIView * top = [[UIView alloc] initWithFrame:CGRectMake(0, -20, tabBar.bounds.size.width, 20)];

top.userInteractionEnabled = NO;

top.backgroundColor = [UIColor clearColor];

top.tag = 999;

[obj addSubview:[[self class] addTopViewToParentView:top topLineColor:lineColor backgroundColor:backgroundColor]];

}

}

}];

}

+(UIView*)addTopViewToParentView:(UIView*)parent topLineColor:(UIColor*)lineColor backgroundColor:(UIColor*)backgroundColor{

////////

//

UIBezierPath *path = [UIBezierPath bezierPath];

//

CGPoint p0 = CGPointMake(0.0, 20);

CGPoint p1 = CGPointMake(parent.bounds.size.width/2.0 - 85, 20);

CGPoint p = CGPointMake(parent.bounds.size.width/2.0, 0);

CGPoint p2 = CGPointMake(parent.bounds.size.width/2.0 + 85, 20);

CGPoint p3 = CGPointMake(parent.bounds.size.width, 20);

//

NSValue *v0 = [NSValue valueWithCGPoint:p0];

NSValue *v1 = [NSValue valueWithCGPoint:p1];

NSValue * v = [NSValue valueWithCGPoint:p];

NSValue *v2 = [NSValue valueWithCGPoint:p2];

NSValue *v3 = [NSValue valueWithCGPoint:p3];

//

NSArray *array = [NSArray arrayWithObjects:v0, v0,  v1, v, v2, v3, v3, nil];

//

[path moveToPoint:p0];

//

for (NSInteger i=0; i<array.count - 3; i++) {

CGPoint t0 = [array[i+0] CGPointValue];

CGPoint t1 = [array[i+1] CGPointValue];

CGPoint t2 = [array[i+2] CGPointValue];

CGPoint t3 = [array[i+3] CGPointValue];

//

for (int i=0; i<100; i++) {

CGFloat t = i/100.0;

CGPoint point = [[self class] getPoint:t p0:t0 p1:t1 p2:t2 p3:t3];

[path addLineToPoint:point];

}

}

//

[path addLineToPoint:p3];

//

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

shapeLayer.lineWidth = 0.5;

shapeLayer.lineCap = @"round";

shapeLayer.strokeColor = [lineColor CGColor];

shapeLayer.fillColor = [backgroundColor CGColor];

shapeLayer.path = [path CGPath];

shapeLayer.strokeStart = 0.0;

shapeLayer.strokeEnd = 1.0;

[parent.layer addSublayer:shapeLayer];

parent.userInteractionEnabled = NO;

return parent;

}

/**

Catmull-Rom算法

根据四个点计算中间点

*/

+ (CGPoint)getPoint:(CGFloat)t p0:(CGPoint)p0 p1:(CGPoint)p1 p2:(CGPoint)p2 p3:(CGPoint)p3 {

CGFloat t2 = t*t;

CGFloat t3 = t2*t;

CGFloat f0 = -0.5*t3 + t2 - 0.5*t;

CGFloat f1 = 1.5*t3 - 2.5*t2 + 1.0;

CGFloat f2 = -1.5*t3 + 2.0*t2 + 0.5*t;

CGFloat f3 = 0.5*t3 - 0.5*t2;

CGFloat x = p0.x*f0 + p1.x*f1 + p2.x*f2 +p3.x*f3;

CGFloat y = p0.y*f0 + p1.y*f1 + p2.y*f2 +p3.y*f3;

return CGPointMake(x, y);

}

@end

 4.下载地址:

https://github.com/liuzhuan155/RadianTabbar或http://code.cocoachina.com/view/1000625

iOS 自定义tabBarController(中间弧形)的更多相关文章

  1. iOS 自定义TabBarController

    转自:http://blog.csdn.net/xn4545945/article/details/35994863 一.自定义的思路 iOS中的TabBarController确实已经很强大了,大部 ...

  2. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  3. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  4. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  5. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  6. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  7. iOS 自定义转场动画

    代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...

  8. iOS 自定义转场动画浅谈

    代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...

  9. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

随机推荐

  1. 好端端的数据结构,为什么叫它SB树呢?

    大家好,今天给大家介绍一个很厉害的数据结构,它的名字就很厉害,叫SB树,业内大佬往往叫做傻叉树.这个真不是我框你们,而是它的英文缩写就叫SBT. SBT其实是英文Size balanced tree的 ...

  2. 蓝桥杯-RP大冒险-未解决

    RP大冒险 问题描述 请尽情使用各种各样的函数来测试你的RP吧~~~ 输入格式 一个数N表示测点编号. 输出格式 一个0~9的数. 样例输入 0 样例输出 X {当且仅当输出仅有一个数X且X为0~9的 ...

  3. 基于Docker搭建pypi私有仓库

    一.搭建 1.准备htpasswd.txt文件 该文件内容包含上传包至仓库时验证的用户名和密码 pip install htpasswd htpasswd -sc htpasswd.txt <u ...

  4. mysql 优化数据类型

    1.更小的通常更好 选择不会超过范围的最小类型 2.简单就好 例如,整型比字符操作代价更低,因为字符集和校对规则(排序规则)使字符比较比整形比较更复杂. 3.尽量避免null 如果查询中包含可为nul ...

  5. 机场&代理商-关系图

    机场&代理商-关系图 思路 ①首先统计机场活跃度Top10的机场名称,以下是我的表结构,以及查询语句 表结构: 查询语句:SELECT * from 2020csale ORDER BY cn ...

  6. SpringBoot 实现微信推送模板

    导读 由于最近手头上需要做个Message Gateway,涉及到:邮件(点我直达).短信.公众号等推送功能,网上学习下,整理下来以备以后使用. 添加依赖 在SpringBoot项目中添加依赖 < ...

  7. Lsi卡和IB卡在CentOS中升级

    LSI 9271 步骤1:准备升级工具和固件包 rpm -ivh MegaCli-8.07.14-1.noarch.rpm [root@phegdata01 ~]# unzip 23-34-0-000 ...

  8. passwrod和shadow文件介绍

    1./etc/passwd #cat /etc/passwdroot:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbi ...

  9. PyQt(Python+Qt)学习随笔:formLayout的layoutRowWrapPolicy属性

    Qt Designer的表单布局(formLayout)中,layoutRowWrapPolicy用于控制表单布局中表单行的标签和输入部件之间是否换行.如图: 上图中蓝色标记圈起来的下拉列表数据是其可 ...

  10. centos7最小安装后——网络配置、常见命令安装,远程连接、yum源安装软件包

    安装环境 #软件:vmware 14 #centos版本:CentOS-7-x86_64-DVD-1810 下载地址: #网络配置:NAT模式 配置 网络配置 #动态获取ip: centos7最小安装 ...