KTCHomeViewController
//MARK: -找到了。
/**这个类用来封住tabbar的显示和隐藏的方法*/
classKTCHomeViewController:BaseViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
overridefuncviewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
//显示tabbar
letappDele =UIApplication.sharedApplication().delegateas!AppDelegate
letctrl = appDele.window?.rootViewController
ifctrl?.isKindOfClass(MainTabBarController.self) ==true{
letmainTabBarCtrl = ctrlas!MainTabBarController
mainTabBarCtrl.showTabbar()
//MARK: -在这里
}
}
overridefuncviewDidAppear(animated:Bool) {
super.viewDidAppear(animated)
//显示tabbar
letappDele =UIApplication.sharedApplication().delegateas!AppDelegate
letctrl = appDele.window?.rootViewController
ifctrl?.isKindOfClass(MainTabBarController.self) ==true{
letmainTabBarCtrl = ctrlas!MainTabBarController
mainTabBarCtrl.showTabbar()
//MARK: -在这里
}
}
overridefuncviewWillDisappear(animated:Bool) {
super.viewWillDisappear(animated)
//隐藏tabbar
letappDele =UIApplication.sharedApplication().delegateas!AppDelegate
letctrl = appDele.window?.rootViewController
ifctrl?.isKindOfClass(MainTabBarController.self) ==true{
letmainTabBarCtrl = ctrlas!MainTabBarController
mainTabBarCtrl.hideTabbar()
}
}
MainTabBarController
老师的 ,用 自定制的 tabBar , 有必有吗? 直接结果 : tabBar 的 显示 和 隐藏 , 也要 写两个 方法。
importUIKit
classMainTabBarController:UITabBarController{
//tabbar背景视图
privatevarbgView:UIView?
//json文件对应的数组
//计算属性
privatevararray:Array>? {
get{
//读文件
letpath =NSBundle.mainBundle().pathForResource("Ctrl.json", ofType:nil)
varmyArray:Array>? =nil
ifletfilePath = path {
letdata =NSData(contentsOfFile: filePath)
ifletjsonData = data {
do{
letjsonValue =tryNSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
ifjsonValue.isKindOfClass(NSArray.self) {
myArray = jsonValueas?Array>
}
}catch{
//程序出现异常
print(error)
returnnil
}
}
}
returnmyArray
}
}
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Swift里面,一般在类的内部调用属性和方法的时候,可以不写self,一般在闭包里面写self
//创建视图控制器
createViewControllers()
}
//创建视图控制器
funccreateViewControllers(){
//视图控制器名字
varctrlNames = [String]()
//tabbar上面的图片
varimageNames = [String]()
//tabbar上面的标题文字
vartitleNames = [String]()
iflettmpArray =self.array{
//json文件的数据解析成功
//并且数组里面有数据
fordictintmpArray {
letname = dict["ctrlName"]
lettitleName = dict["titleName"]
letimageName = dict["imageName"]
ctrlNames.append(name!)
titleNames.append(titleName!)
imageNames.append(imageName!)
}
}else{
ctrlNames = ["CookBookViewController","CommunityViewController","MallViewController","FoodClassViewController","ProfileViewController"]
titleNames = ["食材","社区","商城","食课","我的"]
imageNames = ["home","community","shop","shike","mine"]
}
varvCtrlArray =Array()
foriin0..
//创建视图控制器
letctrlName ="TestKitchen."+ ctrlNames[i]
letcls =NSClassFromString(ctrlName)as!UIViewController.Type
letctrl = cls.init()
//导航
letnavCtrl =UINavigationController(rootViewController: ctrl)
vCtrlArray.append(navCtrl)
}
self.viewControllers= vCtrlArray
//隐藏系统的tabbar
tabBar.hidden=true
//自定制tabbar
self.createCustomTabbar(titleNames, imageNames: imageNames)
}
//自定制tabbar
/*
@param titleNames:文字的数组
@param imageNames:图片的数组
*/
funccreateCustomTabbar(titleNames: [String], imageNames: [String]) {
//1.创建背景视图
bgView=UIView.createView()
bgView?.backgroundColor=UIColor.whiteColor()
//设置边框
bgView?.layer.borderWidth=1
bgView?.layer.borderColor=UIColor.grayColor().CGColor
view.addSubview(bgView!)
//添加约束
bgView?.snp_makeConstraints(closure: {
[weakself]
(make)in
make.left.right.equalTo(self!.view)
make.bottom.equalTo(self!.view)
make.top.equalTo(self!.view.snp_bottom).offset(-49)
})
//2.循环添加按钮
//按钮的宽度
letwidth =kScreenWidth/5.0
foriin0..
//图片
letimageName = imageNames[i]
lettitleName = titleNames[i]
//2.1按钮
letbgImageName = imageName+"_normal"
letselectBgImageName = imageName+"_select"
letbtn =UIButton.createBtn(nil, bgImageName: bgImageName, selectBgImageName: selectBgImageName, target:self, action:#selector(clickBtn(_:)))
bgView?.addSubview(btn)
//MARK: -就是这里
//添加约束
btn.snp_makeConstraints(closure: {
[weakself]
(make)in
make.top.bottom.equalTo(self!.bgView!)
make.width.equalTo(width)
make.left.equalTo(width*CGFloat(i))
})
//2.2文字
letlabel =UILabel.createLabel(titleName, font:UIFont.systemFontOfSize(8), textAlignment: .Center, textColor:UIColor.grayColor())
btn.addSubview(label)
//约束
label.snp_makeConstraints(closure: {
(make)in
make.left.right.equalTo(btn)
make.top.equalTo(btn).offset(32)
make.height.equalTo(12)
})
//设置tag值
btn.tag=300+i
label.tag=400
//默认选中第一个按钮
ifi ==0{
btn.selected=true
label.textColor=UIColor.orangeColor()
}
}
}
funcclickBtn(curBtn:UIButton) {
//1.取消之前选中按钮的状态
letlastBtnView =bgView!.viewWithTag(300+selectedIndex)
iflettmpBtn = lastBtnView {
//上次选中的按钮
letlastBtn = tmpBtnas!UIButton
letlastView = tmpBtn.viewWithTag(400)
iflettmpLabel = lastView {
//上次选中的标签
letlastLabel = tmpLabelas!UILabel
lastBtn.selected=false
lastLabel.textColor=UIColor.grayColor()
}
}
//2.设置当前选中按钮的状态
letcurLabelView = curBtn.viewWithTag(400)
iflettmpLabel = curLabelView {
letcurLabel = tmpLabelas!UILabel
curBtn.selected=true
curLabel.textColor=UIColor.orangeColor()
}
//3.选中视图控制器
selectedIndex= curBtn.tag-300
}
//MARK: -在这里
//显示tabbar
funcshowTabbar(){
UIView.animateWithDuration(1) {//(0.05) {
[weakself]in
self!.bgView?.hidden=false
}
}
//隐藏tabbar
funchideTabbar() {
UIView.animateWithDuration(1) {//(0.05) {
[weakself]in
self!.bgView?.hidden=true
}
}