如何实现UITabBarController的自定义效果,首先要明白自定义和封装代码的原理,才能去实现它。下面这段代码是我自定义UITabBarController时的实现方法
1.创建MyTabBarController继承自UITabBarController
import UIKit
class MyTabBarController: UITabBarController {
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2.自定义一个UIView
var myTabBarView : UIView?
3.在viewDidLoad()方中初始化myTabBarView,并在myTabBarView上添加三个按钮(UIButton)
override func viewDidLoad() {
super.viewDidLoad()
//btnW是每个按钮的宽度,我在这里把屏幕的宽度进行了3等分
let btnW: CGFloat = self.view.frame.size.width/3
//对myTabBarView进行初始化,高度是系统自带的tabBar的高度
self.myTabBarView = ({
let myView = UIView(frame: CGRectMake(0, CGRectGetHeight(self.view.bounds)-self.tabBar.frame.size.height, CGRectGetWidth(self.view.bounds), self.tabBar.frame.size.height))
myView.backgroundColor = UIColor.blueColor()
return myView
})()
//把自定义的标签栏myTabBarView添加到self.view上
self.view.addSubview(self.myTabBarView!)
//标签栏上三个按钮的名字
let namesArray = ["精彩生活","学习工作","休闲娱乐"]
//2.创建按钮(for i in 10...12表示 i >= 10 && i <= 12 ,还可以用for i in 10..<13)
for i in 10...12 {
//实例化按钮button
let btn = UIButton(frame:CGRectMake(btnW*CGFloat(i-10), 0, btnW, self.tabBar.frame.size.height))
//按钮的颜色设置为白色
btn.backgroundColor = UIColor.whiteColor()
//创建Imageview用来添加图片
let imageView = UIImageView(frame:CGRectMake((btn.frame.size.width-25)/2, 5, 25, 25))
//添加图片
imageView.image = UIImage(named: "n"+"\(i-10)")
//把imageView添加到按钮上
btn.addSubview(imageView)
//imageView的tag值
imageView.tag = i+10
//创建标签用来展示按钮的名字
let titleLabel = UILabel(frame:CGRectMake(0, CGRectGetMaxY(imageView.frame), btn.frame.size.width,btn.frame.size.height-CGRectGetMaxY(imageView.frame)))
//文字居中对齐
titleLabel.textAlignment = NSTextAlignment.Center
//标签的背景色设置为白色
titleLabel.backgroundColor = UIColor.whiteColor()
//标签的文字
titleLabel.text = namesArray[i-10]
//文字的颜色
titleLabel.textColor = UIColor.lightGrayColor()
//文字的字体大小
titleLabel.font = UIFont.systemFontOfSize(13)
//把标签添加到按钮上
btn.addSubview(titleLabel)
//标签的tag值
titleLabel.tag = i+20
//给button添加带参的点击事件
btn.addTarget(self, action: #selector(action_BtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
//按钮的tag值
btn.tag = i
//默认第一个按钮被选中
if i == 10 {
//改变按钮的图片
imageView.image = UIImage(named: "s0")
//改变按钮的文字的颜色
titleLabel.textColor = UIColor.purpleColor()
}
//把自定义的按钮添加到自定义的标签栏(myTabBarView)上
self.myTabBarView!.addSubview(btn)
}
setNavs()
}
4.设置导航
func setNavs(){
//初始化所有的controllers
let firstV = FirstViewController()
let twoV = TwoViewController()
let threeV = ThreeViewController()
//把controller 用数组装起来
let cvArray = [firstV,twoV,threeV]
//组装 title 数组
let titlesArray = ["精彩生活","学习工作","休闲娱乐"]
// viewController 的根视图为 UINavagationViewController
var navs = [UINavigationController]()
for (idx, vc) in cvArray.enumerate() {
vc.title = titlesArray[idx]
vc.view.backgroundColor = UIColor.init(white: 0.90, alpha: 1)
vc.automaticallyAdjustsScrollViewInsets = false
let nav = UINavigationController(rootViewController:vc)
nav.navigationBar.barTintColor = UIColor.purpleColor()
nav.navigationBar.tintColor = UIColor.purpleColor()
nav.navigationBar.backgroundColor = UIColor.purpleColor()
nav.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.boldSystemFontOfSize(17),NSForegroundColorAttributeName:UIColor.whiteColor()]
navs.append(nav)
}
//设置索引视图
self.viewControllers = navs
self.selectedIndex = 0
}
5.按钮点击事件事件
func action_BtnClick(sender:UIButton){
//改变被选择的index
self.selectedIndex = sender.tag - 10
//改变 button 的背景
for i in 10...12 {
let imageV:UIImageView = self.myTabBarView!.viewWithTag(i+10) as! UIImageView
let titleL:UILabel = self.myTabBarView!.viewWithTag(i+20) as! UILabel
if i == sender.tag {
imageV.image = UIImage(named: "s"+"\(i-10)")
titleL.textColor = UIColor.purpleColor()
}else{
imageV.image = UIImage(named: "n"+"\(i-10)")
titleL.textColor = UIColor.lightGrayColor()
}
}
}