tabBar的定制

tabBar的定制

class YTTabBarController: UITabBarController {
    
    //MARK: - 属性
    lazy var yt_tabBar:YTTabBar = {
    
        //自己定义的tabBar的大小和系统自带的tabBar的大小一样
        let tempTabBar = YTTabBar(frame:self.tabBar.bounds)
        
        //点击tabBar上的按钮去切换视图控制器
        tempTabBar.changeSelectedIndex = {(index)in
        
            //切换到指定的视图控制器
            self.selectedIndex = index
        }
        
        //将自定义的tabBar贴到系统的tabBar上
        self.tabBar.addSubview(tempTabBar)
        
        
        return tempTabBar
    }()

    //MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.selectedIndex = 0
        
        //创建子视图控制器
        self.addController(OneViewController(), title: "one", imageName: "tiaoman_u", selectImageName: "despicable-me-2-minion-icon-5.png")
        self.addController(TwoViewController(), title: "two", imageName: "huiben_u", selectImageName: "despicable-me-2-minion-icon-4.png")
        self.addController(ThereViewController(), title: "there", imageName: "zhuanti_u", selectImageName: "despicable-me-2-minion-icon-3.png")
        self.addController(FourViewController(), title: "four", imageName: "wode_u", selectImageName: "despicable-me-2-minion-icon-2.png")
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置默认选中的按钮
        self.yt_tabBar.selectedIndex = self.selectedIndex
        
        //改变文字颜色
        self.yt_tabBar.titleColor = UIColor.lightGrayColor()
        
        //移除tabBar上自动添加的所有的子视图
        for item in self.tabBar.subviews {
            //判断子视图的类型是否是YTTabBar
            //isKindOfClass判断指定的对象是否是指定的类型
            if item.isKindOfClass(YTTabBar.self) {
                continue
            }
            
            //将不是YTTabBar的从系统的tabBar上移除
            item.removeFromSuperview()
            
        }
        
    }
    
    
}

//MARK: - 添加子视图控制器
extension YTTabBarController{
    
    ///添加子视图控制器
    func addController(controller:UIViewController,title:String,imageName:String,selectImageName:String){
        
        //设置视图控制器对一个的tabBarItem
        controller.tabBarItem.title = title
        controller.tabBarItem.image = UIImage.init(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        controller.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        //将视图控制器添加到tabBarController中
        self.addChildViewController(controller)
        
        //在yt_tabBar上创建对应按钮
        self.yt_tabBar.addButtonWithItem(controller.tabBarItem)
        
    }
}

==============TabBar的定制========================
class YTTabBar: UIView {

    
    //MARK: - 属性
    ///1.当前选中的下标
    var selectedIndex = 1
    
    ///2.声明闭包用来传值
    var changeSelectedIndex:((Int)->Void)? = nil
    
    ///3.设置按钮的文字颜色
    var titleColor = UIColor.blackColor()
    
}

//MARK: - 添加按钮
extension YTTabBar{

    ///添加按钮
    func addButtonWithItem(item:UITabBarItem) {
        
        //创建按钮
        let btn = YTTabBarButton(tabBarItem: item)
        //添加点击事件
        btn.addTarget(self, action: "btnAction:")
        
        //设置按钮的tag值
        btn.tag = 100+self.subviews.count
        
//        //判断是否是需要选中按钮
//        if self.selectedIndex == self.subviews.count{
//            
//            btn.isSelected = true
//        }
        
        //添加到界面上
        self.addSubview(btn)
    }
}

//MARK: - 按钮点击
extension YTTabBar{

    func btnAction(btn:YTTabBarButton){
        
        //将原来处于选中状态的按钮变成非选中状态
        let selectBtn = self.viewWithTag(100+self.selectedIndex) as! YTTabBarButton
        selectBtn.isSelected = false
        
        //将当前按下的按钮变成选中状态
        btn.isSelected = true
        //更新选中下标
        self.selectedIndex = btn.tag - 100
        
        //通知tabBarController切换视图控制器
        self.changeSelectedIndex!(self.selectedIndex)
        
    }
}

//MARK: - 计算子视图的frame
extension YTTabBar{

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //通用
        let btnW = self.frame.size.width / CGFloat(self.subviews.count)
        let btnH = self.frame.size.height
        let btnY:CGFloat = 0
        
        //遍历拿到所有的按钮
        for (i,item) in self.subviews.enumerate() {
            
            let btn = item as! YTTabBarButton
            
            let btnX = CGFloat(i) * btnW
            //1.设置frame
            item.frame = CGRectMake(btnX, btnY, btnW, btnH)
            
            //2.设置默认选中的按钮
            if i == self.selectedIndex {
                
                btn.isSelected = true
            }
            //3.设置按钮颜色
            btn.titleLabel.textColor = self.titleColor
            
        }
        
        
        
    }
}
==========button的定制=================
//自定义控件:
//1.声明所有的子视图的属性
//2.在构造方法中将子视图添加到界面上
//3.计算子视图的frame


//1.小图
//2.文字
//3.大图
class YTTabBarButton: UIView {

    //MARK: - 属性
    //1.小图
    let smallImageView = UIImageView()
    //2.文字
    let titleLabel = UILabel()
    //3.大图
    let bigImageView = UIImageView()
    
    ///4.按钮的状态
    var isSelected = false{
    
        didSet{
            
            if isSelected == true {
                
                self.bigImageView.hidden = false
            }else{
                self.bigImageView.hidden = true
            }
        }
    }
    
    //5.保存添加事件相关属性
    var target: AnyObject? = nil
    var action: Selector? = nil
    

    //MARK: - 构造方法
    init(tabBarItem:UITabBarItem){
        super.init(frame: CGRectZero)
        //1.小图
        self.addSubview(self.smallImageView)
        self.smallImageView.image = tabBarItem.image
        self.smallImageView.contentMode = .Center
        //2.文字
        self.addSubview(self.titleLabel)
        self.titleLabel.text = tabBarItem.title
        self.titleLabel.textAlignment = .Center
        //3.大图
        self.addSubview(self.bigImageView)
        self.bigImageView.image = tabBarItem.selectedImage
        self.bigImageView.contentMode = .Center
        self.bigImageView.hidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: - 添加点击事件
extension YTTabBarButton{

    ///添加事件
    func addTarget(target:AnyObject,action:Selector){
        
        self.target = target
        self.action = action
    }
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        
        if self.target == nil {
            return
        }
        
         if (self.target!.respondsToSelector(self.action!) == true){
        
            self.target!.performSelector(self.action!, withObject: self)
        }else{
        
            print("按钮点击方法没有实现")
        }
        
    }
    
    
}

//MARK: - 计算frame
extension YTTabBarButton{

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //通用
        let btnW = self.frame.size.width
        let btnH = self.frame.size.height
        let imageProportion = CGFloat(4)/5
        let beyondH:CGFloat = 25
        //1.小图
        let smallX:CGFloat = 0
        let smallY:CGFloat = 0
        let smallW = btnW
        let smallH = btnH * imageProportion
        self.smallImageView.frame = CGRectMake(smallX, smallY, smallW, smallH)
        //2.文字
        let titleX: CGFloat = 0
        let titleY: CGFloat = smallH
        let titleW = btnW
        let titleH = btnH * (1 - imageProportion)
        self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH)
        //3.大图
        let bigX: CGFloat = 0
        let bigY = -beyondH
        let bigW = btnW
        let bigH = btnH + beyondH
        self.bigImageView.frame = CGRectMake(bigX, bigY, bigW, bigH)
        
        
        //判断当前按钮是否处于选中状态
        if self.isSelected {
            
            self.bigImageView.hidden = false
        }
        
    }
}

你可能感兴趣的:(tabBar的定制)