TabBar自定义方式(一)
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自定义方式(一)的更多相关文章
- tabBar自定义
有时系统的tabBar并不能满足我们的开发需求: 这时,我们需要自定义一个tabBar.直接上代码: // 在tabBarController中用KVC更换掉系统tabBar [self setVal ...
- javade多任务处理之Executors框架(线程池)实现的内置几种方式与两种基本自定义方式
一 Executors框架(线程池) 主要是解决开发人员进行线程的有效控制,原理可以看jdk源码,主要是由java.uitl.concurrent.ThreadPoolExecutor类实现的,这里只 ...
- bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式)
bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式) 一.总结 一句话总结:通过监听滚动的高,判断滚动的高是否大于元素距离顶端的距离 1.如何知道屏幕滚动的高? st=$ ...
- iOS tabbar 自定义小红点 消息显示,定制边框、颜色、高宽
一般我们需要显示消息数,会利用到系统提供的api UIApplication.sharedApplication().applicationIconBadgeNumber = 10 但如果我们不想显示 ...
- app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据
随着ArcGIS 10.3的正式发布,Esri推出了新的紧凑型缓存格式以增强用户的访问体验.新的缓存格式下,Esri将缓存的索引信息.bundlx包含在了缓存的切片文件.bundle中.具体如下图所示 ...
- 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client
一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...
- iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...
- tabBar隐藏方式
如果是从A push到B,并且把A的一个东西传到B,那么在push时就要隐藏tabBar,并且要在B ViewController设置一个接收A传到的属性. 这种方式一般用在表格点选,要把表格点选的内 ...
随机推荐
- 虚拟机Centos开机以后,有eth0网卡,但是没有IP,Determine IP information for eth0.. no link present check cable
Determine IP information for eth0.. no link present check cable 如果你的VMware虚拟机centos6.5使用NAT模式,开机以后,使 ...
- 平面上的地图搜索--Java学习笔记(四)
版权声明: 本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处. 这一个月以来,都在学习平面上的地图搜索,主 ...
- Spring AOP的解读
一.为什么会有AOP 在日常的开发中经常会有这样一种场景,支付相关的业务中经常需要记录日志,而记录的日志大体相同:这样就会造成支付组件和日志组件强关联,耦合在一起了.而AOP的出现就是为了解决这种问题 ...
- 整型转字符串(convert int to char)优化实践
0. 前言 其实基本都没什么机会做这么一个基础的优化,一般基础库里就有函数可以直接拿来用. 这里以snprintf为基准,给大家展示一下每一个优化带来的些许收益. 1. 优化过程 1.最初使用的是sn ...
- angular : ng-options
基本调用,得到name 属性 <select ng-model="target" ng-options="obj.name as obj.name for obj ...
- 每天一个linux命令(41)--ping命令
Linux系统的 ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性,它通过发送 ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST t ...
- nodeJS中读写文件方法的区别
导言:nodejs中所有与文件相关的操作都在fs模块中,而读写操作又是我们会经常用到的操作,nodejs的fs模块针对读操作为我们提供了readFile,read, createReadStream三 ...
- Docker存储驱动之OverlayFS简介
简介 OverlayFS是一种和AUFS很类似的文件系统,与AUFS相比,OverlayFS有以下特性: 1) 更简单地设计: 2) 从3.18开始,就进入了Linux内核主线: 3) 可能更快一些. ...
- ThinkPHP框架知识的注意点
ThinkPHP框架 访问入口文件后在application文件夹中会出现一些文件夹,其中的home文件夹是前端模块,也可以在application文件夹中新建文件夹.home文件夹模块中Conf文件 ...
- h5标签基础 table表格标签
一.表格的定义:用于有规范的显示数据. 二.基本组成: 行<tr>/列<td>/表头<caption>/表标题<th> eg: <table> ...