• 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

     1 //
     2 //  ZSTabBar.h
     3 //  自定义TabBar
     4 //
     5 //  Created by HYYT_IOS_ONE on 16/2/29.
     6 //  Copyright © 2016年 zhousheng. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 
    12 /**
    13  *创建协议
    14  */
    15 @protocol ZSTabBarDelegate <NSObject>
    16 @optional
    17 -(void)buttonWithStatue:(UIButton*)button;
    18 
    19 @end
    20 
    21 @interface ZSTabBar : UIView
    22 
    23 +(instancetype)tabBar;
    24 
    25 @property(nonatomic,strong)NSMutableArray*items;
    26 
    27 @property(nonatomic,strong)NSMutableArray*tabarItems;
    28 
    29 @property(nonatomic,weak)id<ZSTabBarDelegate>delegate;
    30 
    31 @end

    ZSTabBar.m

     1 //
     2 //  ZSTabBar.m
     3 //  自定义TabBar
     4 //
     5 //  Created by HYYT_IOS_ONE on 16/2/29.
     6 //  Copyright © 2016年 zhousheng. All rights reserved.
     7 //
     8 
     9 #import "ZSTabBar.h"
    10 #import "UIView+ZSFrame.h"
    11 #import "TabBarGlobleDefine.h"
    12 
    13 @implementation ZSTabBar
    14 
    15 
    16 
    17 +(instancetype)tabBar
    18 {
    19     return [[self alloc]init];
    20 }
    21 
    22 -(void)willMoveToSuperview:(UIView *)newSuperview
    23 {
    24     CGFloat tabBarH=49.0;
    25     CGFloat tabBarW=newSuperview.bounds.size.width;
    26     CGFloat tabBarX=0;
    27     CGFloat tabBarY=newSuperview.bounds.size.height-tabBarH;
    28     
    29     self.frame=CGRectMake(tabBarX, tabBarY, tabBarW, tabBarH);
    30 
    31 }
    32 
    33 
    34 -(NSMutableArray*)tabarItems
    35 {
    36     if (!_tabarItems) {
    37         _tabarItems=[NSMutableArray array];
    38     }
    39     
    40     return _tabarItems;
    41 }
    42 //创建一个set方法
    43 -(void)setItems:(NSMutableArray *)items
    44 {
    45     for (int i=0; i<items.count; i++) {
    46         UIButton*button=items[i];
    47         button.tag=1000+i;
    48         CGFloat btnW=kScreenWith/items.count;
    49         CGFloat btnH=49;
    50         CGFloat btnX=i*btnW;
    51         CGFloat btnY=0;
    52         button.frame=CGRectMake(btnX, btnY, btnW, btnH);
    53         
    54         [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    55         [self addSubview:button];
    56         [self.tabarItems addObject:items[i]];
    57     }
    58     //默认情况下选中第一个button;
    59     [self buttonClick:items[0]];
    60 }
    61 
    62 
    63 
    64 -(void)buttonClick:(UIButton*)button
    65 {
    66     for (int i=0; i<self.tabarItems.count; i++) {
    67         UIButton*button=(UIButton*)self.tabarItems[i];
    68         button.enabled=YES;
    69     }
    70 
    71     
    72     [_delegate buttonWithStatue:button];
    73 
    74 }
    75 
    76 
    77 @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

     1 //
     2 //  ZSOneView.m
     3 //  自定义TabBar
     4 //
     5 //  Created by HYYT_IOS_ONE on 16/3/2.
     6 //  Copyright © 2016年 zhousheng. All rights reserved.
     7 //
     8 
     9 #import "ZSOneView.h"
    10 
    11 @implementation ZSOneView
    12 
    13 - (instancetype)initWithFrame:(CGRect)frame
    14 {
    15     self = [super initWithFrame:frame];
    16     if (self) {
    17         
    18         self.backgroundColor=[UIColor grayColor];
    19     }
    20     return self;
    21 }
    22 
    23 
    24 
    25 //将移动到父视图的时候调用
    26 -(void)willMoveToSuperview:(UIView *)newSuperview
    27 {
    28     self.frame=newSuperview.bounds;
    29 }
    30 
    31 @end

    ZSTwoView 和ZSThree同ZSone

    演示效果

  • 相关阅读:
    Tomcat 配置 login 和 gas
    Mac系统终端命令行不执行命令 总出现command not found解决方法
    NodeJS入门---nodejs详细安装步骤
    Android UI 自动化-Android环境安装
    UI自动化-Chrome元素定位插件CreateXpath的安装及使用
    eclipse解决中文乱码
    pytest的allure的环境配置
    pytest基础简介及实践举例
    Appium 工作原理及 Desired Capabilities
    Appium_adb常用命令总结
  • 原文地址:https://www.cnblogs.com/qitiandasheng/p/5238812.html
Copyright © 2020-2023  润新知