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传到的属性. 这种方式一般用在表格点选,要把表格点选的内 ...
随机推荐
- 一个不错的windows编程网址
http://www.zklmc.com/ 含有MFC,C#,web开发资料
- maven项目发布不成功的问题
MyEclipes 里面有好多的项目,有些项目是插件有些是组件, 就是有些项目是被依赖的项目,有些事项目的主体,被依赖的项目需要打成jar 包放在maven的中央仓库里面,也是所说的maven的 ...
- sqlserver的一些小知识点
1.高效分页sql和储存过程 select top 每页条数 * from ( select ROW_NUMBER() over (order by id)as nid ,* from table01 ...
- Javascript把数据从一个页面的层传递到另一个页面层里面
背景:昨天头脑发热投了某一家国企的计算机类岗位(说是有前端岗位),通过找同学内推,虽然也笔试了一大堆题目(行测题,计算机网络,http协议,英译汉,古诗文默写,自己把品质排序并且进行200字以上的阐述 ...
- JAVA: List用法
1.List中可以添加任何对象,包括自己定义的新的类. class Person{.....}上面定义了一个Person类,下面看好如何使用ListPerson p1=new Person();Per ...
- 腾讯云数据库团队:浅谈如何对MySQL内核进行深度优化
作者介绍:简怀兵,腾讯云数据库团队高级工程师,负责腾讯云CDB内核及基础设施建设:先后供职于Thomson Reuters和YY等公司,PTimeDB作者,曾获一项发明专利:从事MySQL内核开发工作 ...
- Python自动生产表情包
作为一个数据分析师,应该信奉一句话--"一图胜千言".不过这里要说的并不是数据可视化,而是一款全民向的产品形态--表情包!!!! 表情包不仅仅是一种符号,更是一种文化--是促进社交 ...
- 制作流程图,activity,好不容易找到的
Star UML指导手册 Module by: Stephen Wong 原著:Stephen Wong 翻译:火猴 1. 综述:http://pan.baidu.com/s ...
- MyBatis:学习笔记(1)——基础知识
MyBatis:学习笔记(1)--基础知识 引入MyBatis JDBC编程的问题及解决设想 ☐ 数据库连接使用时创建,不使用时就释放,频繁开启和关闭,造成数据库资源浪费,影响数据库性能. ☐ 使用数 ...
- linux下mysql的大小写是否区分设置
转:http://blog.csdn.net/qq_29246225/article/details/52293549 一.Linux中MySQL大小写详情:1.数据库名严格区分大小写2.表名严格区分 ...