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. SaberRD之蒙特卡罗分析(一)

    [声明]本博文的大部分内容摘录于网络,本人按照自己的思维习惯和文字风格进行了重新整理以便于理解和记忆. 鉴于篇幅,我打算先对蒙特卡罗分析的基本思想和历史渊源做一下简单的梳理,然后在下一篇博文中介绍Sa ...

  2. hibernate分页模糊查询

    在web项目中,显示数据一般采用分页显示的,在分页的同时,用户可能还有搜索的需求,也就是模糊查询,所以,我们要在dao写一个可以分页并且可以动态加条件查询的方法.分页比较简单,采用hibernate提 ...

  3. iOS runtime的应用实例

      一直想弄明白runtime是怎么回事,因为面试的时候这是一道必备问题,但是平时用的机会真的少之又少,我一度以为runtime只是用来装13的利器,没什么卵用.但是随着学习的增多,发现runtime ...

  4. Ionic2中集成腾讯Bugly之自定义插件

    Ionic2混合开发,入坑系列:Ionic2中集成腾讯Bugly之自定义插件 1.编写Bugly.js代码 var exec = require('cordova/exec'); module.exp ...

  5. spring事务源码解析

    前言 在spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!最后遗留了一个问题:spring是怎么样保证事务一致性的? 当然,spring事务内容挺多的,如果都要讲的话要花很长时间, ...

  6. 或许是介绍Android Studio使用Git最详细的文章

    欢迎访问我的个人博客转发请注明出处:http://www.wensibo.top/2017/03/12/GitOnAS/ 前言 本文较长,图片很多很多,流量党慎入 使用Git已经有一段时间了,但是之前 ...

  7. KoaHub平台基于Node.js开发的Koa的调试实用程序

    debug small debugging utility debug tiny node.js debugging utility modelled after node core's debugg ...

  8. SDWebImage下载图片的使用

    第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 第二步,在需要的地方导入头文件 1 #import "UII ...

  9. swift -- 基础

    swift -- 基础 1.常量和变量 常量: let 变量: var 2.声明常量和变量 常量的声明: let let  a = 1         //末尾可以不加分号,等号两边的空格必须对应(同 ...

  10. Android Crash 全局捕获

    Android Crash 全局捕获 首先应该明白的一点是,Android在崩溃后会重新启动崩溃时的那个Activity,如果你的Activity在初始化的时候就直接崩溃,那么你将连续得到 Crash ...