iOS自定义tabBar

前段时间我们UI设计师设计了一个与系统自带样式的app的底部tabbar,它除了中间按钮要凸出来,整个tabbar的边部也是不贴边的。要做到这样的效果,就要对系统的babbar进行自定义。UITabBarController中有UITabBar这个类,这个类就是底部导航的关键控件类。

1、创建TextTabBar继承UITabBar

#import "TextTabBar.h"#define SCREEN_WIDTH                [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT               [[UIScreen mainScreen] bounds].size.height@interface TextTabBar ()@property (nonatomic, strong) UIButton       *centerButton;
@property (nonatomic, strong) UIImageView    *tabbarBgImage;@end@implementation TextTabBar- (instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if(self) {[self addSubview:self.tabbarBgImage];// 去除顶部横线[self setBackgroundImage:[UIImage new]];[self setShadowImage:[UIImage new]];}return self;
}- (void)layoutSubviews{[super layoutSubviews];// 重设tabBar的位置self.frame = CGRectMake(0, SCREEN_HEIGHT- 67-10, SCREEN_WIDTH, 67);// 设置中间按钮CGFloat width = 72;self.centerButton.frame = CGRectMake(0 , -15, 45, 45);self.centerButton.center = CGPointMake(SCREEN_WIDTH/2, 15);// 设置其他tabbarbtn的frameCGFloat tabBarButtonW = (SCREEN_WIDTH-40)/5;CGFloat tabBarButtonIndex = 0;for (UIView *child in self.subviews) {Class class = NSClassFromString(@"UITabBarButton");if ([child isKindOfClass:class]) {CGRect frame = CGRectMake(tabBarButtonIndex * tabBarButtonW+20, 3, tabBarButtonW, 49);child.frame = frame;tabBarButtonIndex ++;}}
}- (UIButton *)centerButton {if (_centerButton == nil) {_centerButton = [UIButton buttonWithType:UIButtonTypeCustom];[self addSubview:_centerButton];_centerButton.backgroundColor = [UIColor redColor];_centerButton.layer.cornerRadius = 45/2;_centerButton.layer.masksToBounds = YES;_centerButton.adjustsImageWhenHighlighted = false;[_centerButton addTarget:self action:@selector(centerTabBarBtnEvent) forControlEvents:UIControlEventTouchUpInside];}return _centerButton;
}- (UIImageView *)tabbarBgImage {if (_tabbarBgImage == nil) {_tabbarBgImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 67)];_tabbarBgImage.image = [UIImage imageNamed:@"home_tabbar_bg"];}return _tabbarBgImage;
}// 中间按钮点击事件
- (void)centerTabBarBtnEvent{}

2、 在UITabBarController中去重设tabbar这个属性

因为tabbar是属于UITabBarController的私有属性,所以我们不能用“.”语法对UITabBarController的tabbar直接设值,需要用KVC的"- (void)setValue:(nullable id)value forKey:(NSString *)key; " 方法对UITabBarController的tabbar设值。

  #import "TextTabBarController.h"
@interface TextTabBarController ()@end@implementation TextTabBarController- (void)viewDidLoad {[super viewDidLoad];TextTabBar *tabBar = [[TextTabBar alloc] init];[self setValue:tabBar forKey:@"tabBar"];}

3、给tabbar的添加控制器

#import "TextTabBarController.h"
#import "TextController.h"@interface TextTabBarController ()@end@implementation TextTabBarController- (void)viewDidLoad {[super viewDidLoad];TextTabBar *tabBar = [[TextTabBar alloc] init];[self setValue:tabBar forKey:@"tabBar"];// 添加tabbar控制器[self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar1" image:@"home_tabbar_first" selectedImage:@"home_tabbar_first1"  tag:0];[self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar2" image:@"home_tabbar_travel" selectedImage:@"home_tabbar_travel1" tag:1];[self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"" image:@"" selectedImage:@"" tag:2];[self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar3" image:@"home_tabbar_work" selectedImage:@"home_tabbar_work1" tag:3];[self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar4" image:@"home_tabbar_mine" selectedImage:@"home_tabbar_mine1" tag:4];}/***  初始化一个子控制器**  @param vc            子控制器*  @param title         标题*  @param image         图标*  @param selectedImage 选中的图标*/
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage tag:(NSInteger)tag
{vc.tabBarItem.title = title;vc.tabBarItem.tag = tag;if (image.length) { // 图片名有具体值vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];}[self addChildViewController:vc];
}

效果

iOS自定义tabBar相关推荐

  1. iOS开发之模仿简书App自定义TabBar详解

    先来看看效果图吧 然后我们再来一步一步看看代码 1.首先页面下面TabBar的Button需要自定义,把Button的文字放在图片下面 //更换文字图片的位置,最主要的就是实现以下两个方法,重写父类方 ...

  2. ios tabbar插件_iOS自定义TabBar

    iOS自定义TabBar一般有两种方式: 创建一个类继承字UITabBar,在layoutSubviews方法中重新调整按钮的位置,再通过[self setValue:tabBar forKeyPat ...

  3. 从0开始架构一个IOS程序 ——04— UITabBarController和自定义TabBar 搭建主页面

    从0开始架构一个IOS程序 04 UITabBarController 搭建主页面 Mac OSX 10.11 之后 效果 1 首先创建自定义TabBar 1.1 WISHomeTabBarView. ...

  4. IOS开发-关于自定义TabBar条

    今天在做项目的时候,突然有一个模块需要自定义TabBar条. 在平常很多做项目的时候,都没有去自定义过,大部分都是使用系统自带的.今天整理一个自定义TabBar条的步骤. 首先看下我们最终实现的效果: ...

  5. iOS开发 tabbar自定义转场动画

    1.小记 关于自定义转场动画,只要你理清他的"套路",你就可以随心所欲地自定义了. 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你 ...

  6. iOS开发-实现TabBar中间凸起按钮、不规则按钮(自定义TabBar)

    效果: PS:这里需要用到UIView一个分类的一些属性,参考http://blog.csdn.net/doubleface999/article/details/79085764,图标素材等自行上网 ...

  7. 前置机上如何地址转换_canvas原生层级较高,遮盖自定义tabbar,转换为图片解决...

    本文章基于wepy编写 (1)问题描述: 微信小程序用echarts画统计图:在微信调试工具中,无问题:真机上出现统计图浮在底部自定义tabbar上,并且小程序中使用的第三方dropmenu的遮盖层也 ...

  8. 适配iOS 13 tabbar 标题字体不显示以及返回变蓝色的为问题

    // 适配iOS 13 tabbar 标题字体不显示以及返回变蓝色的为问题 if (@available(iOS 13.0, *)) {//[[UITabBar appearance] setUnse ...

  9. 一劳永逸,iOS 自定义 ActionSheet 封装流程

    原文链接:http://www.jianshu.com/p/cfb87a7db7b1 本文为 iOS 自定义视图封装<一劳永逸>系列的第四期,旨在提供封装思路,结果固然重要,但理解过程才最 ...

最新文章

  1. 模拟器真机环境_Appium+python自动化(二)- 环境搭建—下(超详解)
  2. 探讨8.0版本下后台service存活机制及保活
  3. R语言观察日志(part6)--初识rMarkdown
  4. 使用Spring数据和Thymeleaf实现Bootstrap分页
  5. Spark 性能优化指南(官网文档)
  6. 小熊的人生回忆(二)
  7. android listview 刷新不正确,Android中设置ListView内容刷新问题
  8. php跨进程内存共享,进程管理与内存共享
  9. 大学生发展规划与就业指导(三)
  10. (译)如何使用spritehelper和levelhelper教程:引子
  11. C语言生命游戏源代码
  12. nodejs调用阿里云盾身份证二要素验证
  13. SpringCloud学习笔记7——初级篇之服务降级
  14. flac格式歌曲如何转换成mp3格式,flac转mp3详细图文教程 1
  15. 顾盼华发鸿蒙怦然而梦是什么意思,最美的承诺情话
  16. 图像配准(Image Registration)——深度学习方法
  17. Esper学习笔记三:EPL语法(1)
  18. cocoscreator修改鼠标图标样式
  19. 【Experience Summary】出差布置产线
  20. 优酷视频什么是登录保护?

热门文章

  1. 智能手机市场之争,六个关键词主导
  2. python对建筑设计的作用_Python 与深度学习有哪些与建筑设计相接轨的可能性?...
  3. android recyclerview流式布局,Android FlexboxLayout流式布局
  4. oTMS借势互联网+ 解决物流业“信息孤立”痛点
  5. 算法导论之数学归纳法和递归
  6. 四大最值得推荐的信息安全从业者认证
  7. win10安装mysql5.7缺少MSVCP120.dll
  8. 网红短暂的眼球吸引后如何续命
  9. PyMongo 常见问题
  10. 小白使用一键重装系统好吗?一键重装系统win7教程