Swift - 自定义tabbar的封装

前言:前面三篇博客说到的问题都出自这篇博客中tabbar的封装,而且Object-C版本的封装前面也发过,一样的东西,换了种语言来写。

所以呢,先看下效果:
这里写图片描述

代码简单贴下,大家看看,然后直接到下面找下载地址:


import UIKit

class LHHTabbar: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    /*
     bgImageName和isUse只能有一个使用另一个必为nil,用来设置整个Tabbar背景色
     */
    func creatLHHTabbar(backGroundImage:String?,orUseBackGroundView:Bool,bgViewColor:UIColor ,viewControllerArray:NSArray,normalImageArray:NSArray,selectImageArray:NSArray,itemTitleArray:NSArray,currentTarget:AnyObject,selector:Selector) {
        //创建tabbar背景色,可用图片背景也可用纯色view设置背景
        self.creatLHHTabbarBG(backGroundImage, orUseBackGroundView: orUseBackGroundView, bgViewColor: bgViewColor)
        //创建选择器
        for var i = 0; i<viewControllerArray.count;i++ {
            self.creatLHHTabbarItem(viewControllerArray, normalImageArray: normalImageArray, selectImageArray: selectImageArray, itemTitleArray: itemTitleArray, currentIndex: i, currentTarget: currentTarget, selector: selector)
        }

    }
    func creatLHHTabbarBG(bgImageName:String?,orUseBackGroundView:Bool,bgViewColor:UIColor) {
        if (bgImageName != nil) {
            let imageView = UIImageView(image: UIImage(named: bgImageName!))
            imageView.frame = self.bounds
            self.addSubview(imageView)
        }
        else
        {
            let label = UILabel(frame: self.bounds)
            label.backgroundColor = bgViewColor
            self.addSubview(label)

        }
    }
    func creatLHHTabbarItem(controllerArray:NSArray,normalImageArray:NSArray,selectImageArray:NSArray,itemTitleArray:NSArray,currentIndex:Int,currentTarget:AnyObject,selector:Selector) {

        let bgView = UIView(frame: CGRectMake((self.bounds.size.width / CGFloat(Float(controllerArray.count))) * CGFloat(Float(currentIndex)), 0, self.bounds.size.width / CGFloat(Float(controllerArray.count)), self.bounds.size.height))
        if currentIndex == 0 {

        }
        else
        {
            bgView.backgroundColor = UIColor.clearColor();
        }
        self.addSubview(bgView)

        let imageView = UIImageView(frame: CGRectMake(0, 0, 20, 20))
        imageView.center = CGPointMake(self.bounds.size.width/CGFloat(Float(controllerArray.count))/2, self.bounds.size.height/2-10)
        imageView.tag = currentIndex;
        imageView.userInteractionEnabled = true;
        if currentIndex == 0 {
            imageView.image = UIImage(named: "\(selectImageArray[currentIndex])")
        }
        else
        {
            imageView.image = UIImage(named: "\(normalImageArray[currentIndex])")
        }
        bgView.addSubview(imageView)

        let label = UILabel(frame: CGRectMake(0, imageView.center.y+20/2, self.bounds.size.width/CGFloat(controllerArray.count), self.bounds.size.height-(imageView.center.y+20/2)))

        label.text = "\(itemTitleArray[currentIndex])"
        if currentIndex == 0 {
            label.textColor = UIColor.init(colorLiteralRed: 0.00, green: 0.76, blue: 0.83, alpha: 1.00)
        }
        else
        {
            label.textColor = UIColor.whiteColor()
        }
        label.textAlignment = NSTextAlignment.Center;
        label.font = UIFont.systemFontOfSize(10)
        bgView.addSubview(label)

        let button = UIButton(type: .System)
        button.frame = CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height)
        button.tag = currentIndex;
        button.addTarget(currentTarget, action: selector, forControlEvents: .TouchUpInside)
        bgView.addSubview(button)
    }

}

以上是封装的部分,下载地址:https://github.com/codeliu6572/Swift_CustomTabbar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值