Swift - 自定义tabbar的封装

简介: Swift - 自定义tabbar的封装

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


所以呢,先看下效果:

1.png

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

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

目录
相关文章
|
Swift
swift微博第7天(导航条按钮的封装)
swift微博第7天(导航条按钮的封装)
229 0
swift微博第7天(导航条按钮的封装)
|
JSON 数据格式 iOS开发
Swift学习第十枪-AFNetwoking3.0的封装
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51588829 下面是我的新建的Swift学习交流群,欢迎大家一起来共同学习Swift。
951 0
|
iOS开发
swift4.0 评论弹出键盘的封装
demo地址: https://github.com/weiman152/SwiftCustomInputView 很多应用都有评论功能,评论功能涉及到的问题主要就是两个: 1.键盘弹出的高度 2.动态改变textview的高度 剩下的就是一些动画了。
1208 0
|
JSON Go 数据格式
Swift 3.0封装 URLSession 的GET/SET方法代替 Alamofire
升级到 Swift3.0 之后,新版本的 Alamofire 只支持 iOS 9.0 以上的系统,如果要适配 iOS 8,需要自己封装 URLSession,下面是笔者的方案: 这里使用的是 Swift 自己的原生类型 URLSession,而不是NSURLSession。
1063 0
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
451 2
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
354 1
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
296 5
|
存储 移动开发 Swift
使用Swift进行iOS应用开发:探索现代移动开发的魅力
【8月更文挑战第12天】使用Swift进行iOS应用开发,不仅能够享受到Swift语言带来的简洁、快速、安全的编程体验,还能够充分利用iOS平台提供的丰富资源和强大功能。然而,iOS应用开发并非易事,需要开发者具备扎实的编程基础、丰富的实践经验和不断学习的精神。希望本文能够为您的iOS应用开发之旅提供一些有益的参考和帮助。