創建 viewController, 憑借 json 文件, 任意顯示想展示的內容 ,
不需App 升級
自定制 下面 的 tabBar, 隱藏 系統的, 顯示 自己制造的 view
應該 用 解耦,
何必 UIButton + UILabel,
UIButton ,很強大,
系統的 UITab
import UIKit
class MainTabBarController: UITabBarController {
//tabbar背景視圖
private var bgView: UIView?
//json文件對應的數組
//計算屬性
private var array: Array<Dictionary<String,String>>? {
get {
//讀文件
let path = NSBundle.mainBundle().pathForResource("Ctrl.json", ofType: nil)
var myArray: Array<Dictionary<String,String>>? = nil
if let filePath = path {
let data = NSData(contentsOfFile: filePath)
if let jsonData = data {
do {
let jsonValue = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
if jsonValue.isKindOfClass(NSArray.self) {
myArray = jsonValue as? Array<Dictionary<String,String>>
}
}catch {
//程序出現異常
print(error)
return nil
}
}
}
return myArray
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Swift里面,一般在類的內部調用屬性和方法的時候,可以不寫self,一般在閉包里面寫self
//創建視圖控制器
createViewControllers()
}
//創建視圖控制器
func createViewControllers(){
//視圖控制器名字
var ctrlNames = [String]()
//tabbar上面的圖片
var imageNames = [String]()
//tabbar上面的標題文字
var titleNames = [String]()
if let tmpArray = self.array {
//json文件的數據解析成功
//并且數組里面有數據
for dict in tmpArray {
let name = dict["ctrlName"]
let titleName = dict["titleName"]
let imageName = 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"]
}
var vCtrlArray = Array<UINavigationController>()
for i in 0..<ctrlNames.count {
//創建視圖控制器
let ctrlName = "TestKitchen." + ctrlNames[i]
let cls = NSClassFromString(ctrlName) as! UIViewController.Type
let ctrl = cls.init()
//導航
let navCtrl = UINavigationController(rootViewController: ctrl)
vCtrlArray.append(navCtrl)
}
self.viewControllers = vCtrlArray
//隱藏系統的tabbar
tabBar.hidden = true
//自定制tabbar
self.createCustomTabbar(titleNames, imageNames: imageNames)
}
//自定制tabbar
/*
@param titleNames:文字的數組
@param imageNames:圖片的數組
*/
func createCustomTabbar(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: {
[weak self]
(make) in
make.left.right.equalTo(self!.view)
make.bottom.equalTo(self!.view)
make.top.equalTo(self!.view.snp_bottom).offset(-49)
})
//2.循環添加按鈕
//按鈕的寬度
let width = kScreenWidth/5.0
for i in 0..<imageNames.count {
//圖片
let imageName = imageNames[i]
let titleName = titleNames[i]
//2.1 按鈕
let bgImageName = imageName+"_normal"
let selectBgImageName = imageName+"_select"
let btn = UIButton.createBtn(nil, bgImageName: bgImageName, selectBgImageName: selectBgImageName, target: self, action: #selector(clickBtn(_:)))
bgView?.addSubview(btn)
//MARK: - 就是這里
//添加約束
btn.snp_makeConstraints(closure: {
[weak self]
(make) in
make.top.bottom.equalTo(self!.bgView!)
make.width.equalTo(width)
make.left.equalTo(width*CGFloat(i))
})
//2.2 文字
let label = 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
//默認選中第一個按鈕
if i == 0 {
btn.selected = true
label.textColor = UIColor.orangeColor()
}
}
}
func clickBtn(curBtn: UIButton) {
//1.取消之前選中按鈕的狀態
let lastBtnView = bgView!.viewWithTag(300+selectedIndex)
if let tmpBtn = lastBtnView {
//上次選中的按鈕
let lastBtn = tmpBtn as! UIButton
let lastView = tmpBtn.viewWithTag(400)
if let tmpLabel = lastView {
//上次選中的標簽
let lastLabel = tmpLabel as! UILabel
lastBtn.selected = false
lastLabel.textColor = UIColor.grayColor()
}
}
//2.設置當前選中按鈕的狀態
let curLabelView = curBtn.viewWithTag(400)
if let tmpLabel = curLabelView {
let curLabel = tmpLabel as! UILabel
curBtn.selected = true
curLabel.textColor = UIColor.orangeColor()
}
//3.選中視圖控制器
selectedIndex = curBtn.tag - 300
}
//MARK: - 在這里 ?
//顯示tabbar
func showTabbar(){
UIView.animateWithDuration(1) {//(0.05) {
[weak self] in
self!.bgView?.hidden = false
}
}
//隱藏tabbar
func hideTabbar() {
UIView.animateWithDuration(1) {//(0.05) {
[weak self] in
self!.bgView?.hidden = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}