1.思路:创建一个继承UIView的TabBar类,并将需要的item添加到TabBar上面去,并用代理来处理相应的时间

[self.view bringSubviewToFront:self.oneView];//将这个视图提到前面去

/**

当视图将要添加到对应的父视图的时候调用

*/

-(void)willMoveToSuperview:(UIView *)newSuperview

{

self.frame=newSuperview.bounds;

}

下面是代码片段结构

重要片段

TabBarGlobleDefine.h

//
// TabBarGlobleDefine.h
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/3/3.
// Copyright © 2016年 zhousheng. All rights reserved.
// #ifndef TabBarGlobleDefine_h
#define TabBarGlobleDefine_h #define kScreenWith [UIScreen mainScreen].bounds.size.width #endif /* TabBarGlobleDefine_h */

ViewController.h

//
// ViewController.h
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/2/29.
// Copyright © 2016年 zhousheng. All rights reserved.
//
//
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end

viewController.m

//
// ViewController.m
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/2/29.
// Copyright © 2016年 zhousheng. All rights reserved.
//
//
#import "ViewController.h"
#import "ZSTabBar.h"
#import "ZSOneView.h"
#import "ZSTwoView.h"
#import "ZSThree.h" @interface ViewController ()<ZSTabBarDelegate> @property(nonatomic,strong)ZSOneView*oneView;
@property(nonatomic,strong)ZSTwoView*twoView;
@property(nonatomic,strong)ZSThree*threeView; @property(nonatomic,strong)ZSTabBar*tabBar; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建tabBar
ZSTabBar*tabBar=[ZSTabBar tabBar];
tabBar.backgroundColor=[UIColor grayColor];
tabBar.delegate=self;
//创建UIButton
UIButton*btn1=[[UIButton alloc]init];
btn1.backgroundColor=[UIColor redColor]; [self tabBarItemWithButton:btn1 AndTitle:@"我是tabBar1" AndNorModel:nil AnddisModel:nil]; UIButton*btn2=[[UIButton alloc]init];
btn2.backgroundColor=[UIColor greenColor];
[self tabBarItemWithButton:btn2 AndTitle:@"我是tabBar2" AndNorModel:nil AnddisModel:nil]; UIButton*btn3=[[UIButton alloc]init];
btn3.backgroundColor=[UIColor orangeColor];
[self tabBarItemWithButton:btn3 AndTitle:@"我是tabBar3" AndNorModel:nil AnddisModel:nil];
tabBar.items=@[btn1,btn2,btn3]; [self.view addSubview:tabBar];
self.tabBar=tabBar; } #pragma mark---进行懒加载添加视图
-(ZSOneView*)oneView
{ if (_oneView==nil) {
_oneView=[[ZSOneView alloc]init];
[self.view addSubview:_oneView];
}
return _oneView;
} -(ZSTwoView*)twoView
{
if (!_twoView) {
_twoView=[[ZSTwoView alloc]init];
[self.view addSubview:_twoView];
} return _twoView;
} -(ZSThree*)threeView
{
if (!_threeView) {
_threeView=[[ZSThree alloc]init];
[self.view addSubview:_threeView];
}
return _threeView;
} #pragma mark---ZSTabBarDelegate遵守协议
-(void)buttonWithStatue:(UIButton *)button
{ if (button.tag==1000) { [self.view bringSubviewToFront:self.oneView];
NSLog(@"%ld",button.tag);
button.enabled=NO; }
else if(button.tag==1001){
[self.view bringSubviewToFront:self.twoView];
button.enabled=NO; }
else{
[self.view bringSubviewToFront:self.threeView];
button.enabled=NO; } [self.view bringSubviewToFront:self.tabBar]; } #pragma mark---设置TabBar上Items的样式 -(void)tabBarItemWithButton:(UIButton*)button AndTitle:(NSString*)title AndNorModel:(NSString*)normelColol AnddisModel:(NSString*)disModel
{ [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

ZSTabBar.h

 //
// ZSTabBar.h
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/2/29.
// Copyright © 2016年 zhousheng. All rights reserved.
// #import <UIKit/UIKit.h> /**
*创建协议
*/
@protocol ZSTabBarDelegate <NSObject>
@optional
-(void)buttonWithStatue:(UIButton*)button; @end @interface ZSTabBar : UIView +(instancetype)tabBar; @property(nonatomic,strong)NSMutableArray*items; @property(nonatomic,strong)NSMutableArray*tabarItems; @property(nonatomic,weak)id<ZSTabBarDelegate>delegate; @end

ZSTabBar.m

 //
// ZSTabBar.m
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/2/29.
// Copyright © 2016年 zhousheng. All rights reserved.
// #import "ZSTabBar.h"
#import "UIView+ZSFrame.h"
#import "TabBarGlobleDefine.h" @implementation ZSTabBar +(instancetype)tabBar
{
return [[self alloc]init];
} -(void)willMoveToSuperview:(UIView *)newSuperview
{
CGFloat tabBarH=49.0;
CGFloat tabBarW=newSuperview.bounds.size.width;
CGFloat tabBarX=;
CGFloat tabBarY=newSuperview.bounds.size.height-tabBarH; self.frame=CGRectMake(tabBarX, tabBarY, tabBarW, tabBarH); } -(NSMutableArray*)tabarItems
{
if (!_tabarItems) {
_tabarItems=[NSMutableArray array];
} return _tabarItems;
}
//创建一个set方法
-(void)setItems:(NSMutableArray *)items
{
for (int i=; i<items.count; i++) {
UIButton*button=items[i];
button.tag=+i;
CGFloat btnW=kScreenWith/items.count;
CGFloat btnH=;
CGFloat btnX=i*btnW;
CGFloat btnY=;
button.frame=CGRectMake(btnX, btnY, btnW, btnH); [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
[self.tabarItems addObject:items[i]];
}
//默认情况下选中第一个button;
[self buttonClick:items[]];
} -(void)buttonClick:(UIButton*)button
{
for (int i=; i<self.tabarItems.count; i++) {
UIButton*button=(UIButton*)self.tabarItems[i];
button.enabled=YES;
} [_delegate buttonWithStatue:button]; } @end

ZSOneView.h

//
// ZSOneView.h
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/3/2.
// Copyright © 2016年 zhousheng. All rights reserved.
// #import <UIKit/UIKit.h> @interface ZSOneView : UIView @end

ZSOneView.m

 //
// ZSOneView.m
// 自定义TabBar
//
// Created by HYYT_IOS_ONE on 16/3/2.
// Copyright © 2016年 zhousheng. All rights reserved.
// #import "ZSOneView.h" @implementation ZSOneView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { self.backgroundColor=[UIColor grayColor];
}
return self;
} //将移动到父视图的时候调用
-(void)willMoveToSuperview:(UIView *)newSuperview
{
self.frame=newSuperview.bounds;
} @end

ZSTwoView 和ZSThree同ZSone

演示效果

TabBar自定义方式(一)的更多相关文章

  1. tabBar自定义

    有时系统的tabBar并不能满足我们的开发需求: 这时,我们需要自定义一个tabBar.直接上代码: // 在tabBarController中用KVC更换掉系统tabBar [self setVal ...

  2. javade多任务处理之Executors框架(线程池)实现的内置几种方式与两种基本自定义方式

    一 Executors框架(线程池) 主要是解决开发人员进行线程的有效控制,原理可以看jdk源码,主要是由java.uitl.concurrent.ThreadPoolExecutor类实现的,这里只 ...

  3. bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式)

    bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式) 一.总结 一句话总结:通过监听滚动的高,判断滚动的高是否大于元素距离顶端的距离 1.如何知道屏幕滚动的高? st=$ ...

  4. iOS tabbar 自定义小红点 消息显示,定制边框、颜色、高宽

    一般我们需要显示消息数,会利用到系统提供的api UIApplication.sharedApplication().applicationIconBadgeNumber = 10 但如果我们不想显示 ...

  5. app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)

    首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...

  6. 《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据

    随着ArcGIS 10.3的正式发布,Esri推出了新的紧凑型缓存格式以增强用户的访问体验.新的缓存格式下,Esri将缓存的索引信息.bundlx包含在了缓存的切片文件.bundle中.具体如下图所示 ...

  7. 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client

    一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...

  8. iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)

    首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...

  9. tabBar隐藏方式

    如果是从A push到B,并且把A的一个东西传到B,那么在push时就要隐藏tabBar,并且要在B ViewController设置一个接收A传到的属性. 这种方式一般用在表格点选,要把表格点选的内 ...

随机推荐

  1. [UWP]涨姿势UWP源码——UI布局

    懒癌晚期兼正月里都是过年,一直拖到今天才继续更新.之前的几篇介绍了数据的来源,属于准备工作.本篇我们正式开始构建涨姿势UWP程序的UI界面. 我们这个Hello World程序比较简单,总共只有一个页 ...

  2. fiddler介绍

    先看fiddler 的使用界面和各模块的功能介绍 1请求列表:请求列表中包含了许多信息,从左至右依次为,#(序列号),Result(结果状态码),Prottocol(请求的协议),Host(请求的主机 ...

  3. echarts动态添加数据(饼图为例)

    $.ajax({type : "POST",async : false,url : '${ctx}/basic/bsAllPictureGuarantee/pictJson',da ...

  4. ES6新属性笔记

    一.destructuring--解构赋值 1.数组解构赋值 (1)完全解构 let [a,b,c] = [1,2,3];//普通 console.log(a+":"+b+&quo ...

  5. 关于CSS各种选择器,还有各种引入样式表的区别,import导入样式表,在介绍一些伪类选择器

    (一)CSS选择器: 1.标签选择器:通过HTML的标签名直接选择该标签 2.类选择器:通过.选择器的名称{} 来对添加了class属性的标签进行选中 3.ID选择器:通过#选择器的名称{} 来对添加 ...

  6. ForEach 循环

    在C 标签里面 有个foreach 标签,这个标签是专门来做循环的标签: <c:forEach items="${wekList}"  var="list" ...

  7. vuejs数据双向绑定原理(get & set)

    前端的数据双向绑定指的是view(视图)和model(数据)两者之间的关系:view层是页面上展示给用户看的信息,model层一般是指通过http请求从后台返回的数据.view到model的绑定都是通 ...

  8. [干货来袭]C#7.0新特性(VS2017可用)

    前言 微软昨天发布了新的VS 2017 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下其实2016年12月就已经公布了的C#7 ...

  9. SQLServer索引循环删除

    declare qc_cursor cursor SCROLL OPTIMISTIC Forselect siteName from tb_vhostcheckopen qc_cursordeclar ...

  10. wxWidgets 的 Linux / Windows 混合开发环境

    目标 建立一个开发 GUI 交互界面程序的环境,要求: 以 C/C++ 为主要程序设计语言. 最好能充分利用已有的 MFC 开发经验. 以 Linux 为主要开发环境.可以在 Windows 中编译并 ...