★★★★自定义tabBar★★★★★★★

Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git

前言:

有的时候需求要对tabBar进行自定义的时候,感觉还是有很多不明白的地方,特此写一个demo来记录,大家可以借鉴这个思路仿写一个属于自己的tabBar。我写的并不好,有错误的地方,请大家指出来,我好马上修改,谢谢。

思路是自定义tabBarController中设置2个一个是ViewController子视图和tabBar 在ViewController中设置4个子视图

一、设置所有子视图

//添加所有子视图
- (void)setAllChildViewController
{ //第一页
XLJFirstController *first = [[XLJFirstController alloc] init]; [self setOneChildViewController:first withNormalImage:[UIImage imageNamed:@"tabbar_home"] withSelectedImage:[UIImage imageNamed:@"tabbar_home_selected"] withTitle:@"first"]; //第二页
XLJSecondController *second = [[XLJSecondController alloc] init]; [self setOneChildViewController:second withNormalImage:[UIImage imageNamed:@"tabbar_message_center"] withSelectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"] withTitle:@"second"]; //第四页
XLJFourController *four = [[XLJFourController alloc] init]; [self setOneChildViewController:four withNormalImage:[UIImage imageNamed:@"tabbar_discover"] withSelectedImage:[UIImage imageNamed:@"tabbar_discover_selected"] withTitle:@"four"]; //第五页
XLJFiveController *five = [[XLJFiveController alloc] init]; [self setOneChildViewController:five withNormalImage:[UIImage imageNamed:@"tabbar_profile"] withSelectedImage:[UIImage imageNamed:@"tabbar_profile_selected"] withTitle:@"five"]; }

二、设置自定义tabBar

//自定义tabBar,自定义的方法类似
- (void)setUpTabBar
{
XLJTabBar *tabBar = [[XLJTabBar alloc] initWithFrame:self.tabBar.bounds]; tabBar.backgroundColor = [UIColor lightGrayColor];
tabBar.delegate = self;
tabBar.items = self.items; [self.tabBar addSubview:tabBar];
}

1. 在自定义tabBar的时候,创建XLJTabBar类,让它继承自UIView,这个类要做哪些事情呢?主要做布局的事情,有普通的按钮和一个图片的按钮,他们的事件通过代理的方式来实现。

位置的计算方法(其它方法根据自己的来写):

//调整子控件的位置
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat w = self.bounds.size.width;
CGFloat h = self.bounds.size.height; CGFloat btnX = 0;
CGFloat btnY = 0;
CGFloat btnW = w / (self.items.count + 1);
CGFloat btnH = h; int i = 0;
//设置tabBarButton的frame
for (UIView *tabBarButton in self.buttonmArray) {
if (i == 2) {
i = 3;
}
btnX = i * btnW;
tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
i++;
} //设置添加按钮的位置
self.plusButton.center = CGPointMake(w * 0.5, h * 0.5); }

三、移除系统的tabBarButton的方法

/移除系统自带的tarBar
for (UIView *tabBarButton in tabBarVc.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarButton removeFromSuperview];
}
}

四、处理Navigation手势滑动的问题


#import "XLJNavigationController.h"
#import "UIBarButtonItem+XLJButtonItem.h" @interface XLJNavigationController ()<UINavigationControllerDelegate>
//用于返回手势标记
@property (nonatomic, strong) id popDelegate; @end @implementation XLJNavigationController - (void)viewDidLoad {
[super viewDidLoad]; self.popDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
} //导航控制器跳转完成的时候调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self.viewControllers[0]) {
self.interactivePopGestureRecognizer.delegate = self.popDelegate;
}else{
//实现滑动返回功能
self.interactivePopGestureRecognizer.delegate = nil;
}
}

五、未解决问题appearanceWhenContainedIn在iOS9以后这个就会出现警告的问题,希望知道的朋友帮忙解决.iOS9以后说是用这个但是不会,一用就崩溃了。

 UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UIBarButtonItem class]]];

PS:效果图

自定义tabBar的更多相关文章

  1. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  2. iOS 隐藏自定义tabbar

    iOS  隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...

  3. iOS开发之功能模块--关于自定义TabBar条

    只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...

  4. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  5. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

  6. 第二篇、Swift_自定义 tabbar 的 badgeValue显示样式

    在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色 使用方式: class BKTabBarController: UITabBarCon ...

  7. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

  8. 1行代码为每个Controller自定义“TabBar”-b

    这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...

  9. iOS 轻松实现自定义TabBar

    自定义TabBar的案例网上不少,昨天受到开发小伙伴的影响,尝试了一下非大神的取巧思路:Demo 1.创建RootViewController,后面创建几个继承的VC,将这几个VC添加到TabBarC ...

随机推荐

  1. mybatis_映射查询

    一.一对一映射查询: 第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段.此方法较为简单,企业中使用普遍. <!- ...

  2. Angular2开发笔记

    Problem 使用依赖注入应该注意些什么 服务一般用来做什么 指令一般用来做什么 angular2如何提取公共组件 angular2为什么不需要提公共组件 父组件与子组件之间如何通讯 什么时候应该使 ...

  3. spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析

    1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...

  4. AFNetworking 3.0 源码解读(七)之 AFAutoPurgingImageCache

    这篇我们就要介绍AFAutoPurgingImageCache这个类了.这个类给了我们临时管理图片内存的能力. 前言 假如说我们要写一个通用的网络框架,除了必备的请求数据的方法外,必须提供一个下载器来 ...

  5. 《你不知道的JavaScript》整理(四)——原型

    一.[[Prototype]] JavaScript中的对象有一个特殊的[[Prototype]]内置属性,其实就是对于其他对象的引用. var myObject = { a: 2 }; myObje ...

  6. IdentityServer4 使用OpenID Connect添加用户身份验证

    使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证.客户端调用,实现授权. IdentityServer4 目前已更新至1.0 版,在之前的文章中有所介绍.I ...

  7. PHP设计模式(七)适配器模式(Adapter For PHP)

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 如下图(借图): // 设置书的接口 // 书接口 interface BookI ...

  8. GSD_WeiXin(高仿微信)应用源码

    高仿微信计划:已经实现功能 1.微信首页(cell侧滑编辑.下拉眼睛动画.下拉拍短视频.点击进入聊天详情界面) 2.通讯录(联系人字母排序.搜索界面) 3.发现(朋友圈) 4.我(界面) 待实现功能( ...

  9. Spring异步功能

    使用 Spring 的异步功能时,实质是使用的 Servlet3 及以上版本的异步功能. Spring 的异步处理机制需要在 web.xml 中全部的 servlet 和 filter 处配置 < ...

  10. 【腾讯Bugly干货分享】微信终端跨平台组件 mars 系列(二) - 信令传输超时设计

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/9DJxipJaaBC8yC-buHgnTQ 作者简介: ...