zoukankan      html  css  js  c++  java
  • 封装实现一个自己的tabbar

    实现效果:

    原来效果:

     

    实现对应的思路有:

    1.首先你要拥有这样的一个控件CSTabBar,继承自tabbar,这样才能做到重构(废话)

    2.你要在使用CSTabBar使用kvc来实现    [self setValue:newTab forKey:@"tabBar"];  newTab是自己设定的对应的tabbar

    3.就可以开始构造自己的Tabbar了,构造的第一步当然是设置中间按钮的size。

    4.改变对应的tabbar 里面item的大小,和对应点击事件发生的时候你要响应什么东西。

    //
    //  CSTabBar.m
    //  diary
    //
    //  Created by asheng123 on 15/4/11.
    //  Copyright (c) 2015年 asheng123. All rights reserved.
    //
    
    #import "CSTabBar.h"
    @interface CSTabBar()
    @property(nonatomic,weak)UIButton *plusButton;
    @end
    @implementation CSTabBar
    
    -(instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self setPlusButton];
        }
        return self;
    }
    -(void)setPlusButton
    {
        UIButton * plusButton=[[UIButton alloc]init];
        [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [self addSubview:plusButton];
        self.plusButton = plusButton;
    }
    -(void)layoutSubviews
    {
        [super layoutSubviews];
        
        [self setupPlusButtonFrame];
        [self setupallTabBarFrame];
    }
    -(void)setupPlusButtonFrame
    {
        self.plusButton.size = self.plusButton.currentBackgroundImage.size;
        self.plusButton.center = CGPointMake(self.width*0.5, self.height*0.5);
    }
    -(void)setupallTabBarFrame
    {
        int index= 0;
        for (UIView *tabBarButton in self.subviews) {
    //        [tabBarButton isKindOfClassL: NSClassFromString(@"UITabBarButton")];
            BOOL isTabBar =[tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")];
            if (!isTabBar) {
                continue;
            }
            [self setTabBarButtonFrame:tabBarButton withIndex:index];
            [self setTabBarButtonTextColor:tabBarButton withIndex:index];
        
            index++;
        }
        
    }
    //设置每一个tabbar对应的frame
    //传进来的tabbar   也就是当前的一个按钮,那么要给他设定对应的值
    -(void)setTabBarButtonFrame:(UIView *)tabBar withIndex:(int)index
    {
        //首先取大小
        CGFloat tabWidth= self.width/(self.items.count +1);
        CGFloat tabHeight = self.height;
        
        //然后就是给对应控件赋值了
        if (index>=2) {
            tabBar.frame = CGRectMake(tabWidth*(index +1), 0, tabWidth, tabHeight);
        }else
        {
            tabBar.frame = CGRectMake(tabWidth *index, 0, tabWidth, tabHeight);
        }
        
    }
    //设置每一个tabbar对应的背景color
    -(void)setTabBarButtonTextColor:(UIView *)tabBar withIndex:(int)index
    {
        //要给每一个选中的tabbar对应一个background
        //比较重要的就是判断对应的index和当前选中的项目是不是相同
        int currentSelect= (int)[self.items indexOfObject:self.selectedItem];
        //判断对应的是不是选中的项
        for (UILabel *label in tabBar.subviews) {
            if (![label isKindOfClass:NSClassFromString(@"UILabel")]) {
                continue;
            }
            if (currentSelect == index) {
                label.textColor = [UIColor orangeColor];
            }else
            {
                label.textColor = [UIColor blackColor];
            }
        }
    }
    @end

    其中index这个设计的比较巧妙。通过遍历来找到对应的index,判断是否是相同的一个属性,isKindOfClass可以判断,因为这个事对应的子类中的方法,可以判断出是什么调用了这个方法。之间存在着继承关系

    纸上得来终觉浅,绝知此事要躬行
  • 相关阅读:
    深入浅出之正则表达式(二)
    ==和equlas的区别
    温故知新 javascript 正则表达式
    SQL Server高级内容之子查询和表链接
    C语言访问webservice小例子
    jquery菜单伸缩效果
    电影资源下载网站推荐
    百度云使用迅雷下载资源
    字符编码
    7_2 利用 const 引用避免复制
  • 原文地址:https://www.cnblogs.com/asheng/p/4418507.html
Copyright © 2011-2022 走看看