TabBar自定义方式(一)

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

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

/**

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

*/

-(void)willMoveToSuperview:(UIView *)newSuperview

{

self.frame=newSuperview.bounds;

}

下面是代码片段结构

TabBar自定义方式(一)

重要片段

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

演示效果

上一篇:web简单连接html文件测试


下一篇:php mysql语句预编译(preparestatement)