Swift自定义tabbar

本人第一篇文章,希望大家多多支持,目前支持swift4.1,废话不多说,直接上代码

继承 UIButton

publiclet LLLUibuttonRatio = 0.7

class LLButton:UIButton{

    overrideinit(frame:CGRect) {

        super.init(frame: frame)

        self.imageView?.contentMode = .center

        self.titleLabel?.textAlignment = .center

        self.titleLabel?.font = UIFont.systemFont(ofSize: 11)

    }


    overridevarisHighlighted:Bool{

        set{


        }

        get{

            return false

        }

    }


    overridefuncimageRect(forContentRect contentRect:CGRect) ->CGRect{

        let imageW:CGFloat= contentRect.size.width

        let imageH:CGFloat = contentRect.size.height * CGFloat(LLLUibuttonRatio)

        return CGRect(x: 0,y: 0,width: imageW,height: imageH)

    }


    overridefunctitleRect(forContentRect contentRect:CGRect) ->CGRect{

        let titleY:CGFloat = contentRect.size.height * CGFloat(LLLUibuttonRatio)

        let titleW:CGFloat= contentRect.size.width

        let titleH:CGFloat= contentRect.size.height- titleY - 8

        return CGRect(x: 0,y: titleY,width: titleW,height: titleH)

    }


    requiredinit?(coder aDecoder:NSCoder) {

        fatal Error("init(coder:) has not been implemented")

    }


}


继承 UIView


//声明代理方法

protocolLLMyTabbarDelegate:NSObjectProtocol{

     func tabbarDidSelectedButtomFromto(tabbar:LLMyTabbar,from:Int,to:Int)

}

class LLMyTabbar:UIView{

    //delegate  要写在class里面,否则无效

    var delegate:LLMyTabbarDelegate?

    var nomarlButton =UIButton()

    var seletedButton =UIButton()


    overrideinit(frame:CGRect) {

        super.init(frame: frame)

    }


    requiredinit?(coder aDecoder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }


    funcaddTabBarButtonWithItem(item:UITabBarItem)->Void{


        letbutton:LLButton=LLButton(type: .custom)

        self.addSubview(button)


        button.setTitleColor(UIColor.gray, for: .normal)

        button.setTitleColor(colorWithHexString(Color_Value: "#ff5f53", alpha: 1), for: UIControl.State.selected)

        button.setTitle(item.title, for: .normal)

        button.setImage(item.image, for: .normal)

        button .setImage(item.selectedImage, for: .selected)

        button.backgroundColor = UIColor.white

        button.addTarget(self, action:#selector(buttonClick(_:)), for: .touchUpInside)


        ifself.subviews.count== 1 {

            self.buttonClick(button)

        }


    }

    //button 调用方法传参使用  需注意

   @objcfuncbuttonClick(_sender:UIButton) {


        //调用代理

        if(delegate?.responds(to:Selector(("tabbarDidSelectedButtomFromto"))) !=nil)  {

            delegate?.tabbarDidSelectedButtomFromto(tabbar: self, from: seletedButton.tag, to: sender.tag)

        }

        seletedButton.isSelected = false

        sender.isSelected=true

        seletedButton= sender


    }


    overridefunclayoutSubviews() {

        super.layoutSubviews()


        forvarindexin0...(self.subviews.count-1) {

            letbutton:LLButton=self.subviews[index]as!LLButton

            let number:Int = Int(CGFloat(kScreenWidth))/Int(self.subviews.count)

            let width:CGFloat = CGFloat(self.frame.size.width)/CGFloat(Int(self.subviews.count))

            letX:CGFloat=CGFloat(index * number)

            letY:CGFloat= 0.0

            button.frame=CGRect(x: X,y: Y,width: width,height:self.frame.size.height)

            button.tag= index 

            index = index + 1


        }

    }


}


Demo地址 https://github.com/1461139257/customTabbarSwift.git

希望大家会喜欢!有问题可以下方留言,谢谢

你可能感兴趣的:(Swift自定义tabbar)