使用plist文件來加載控制器以及圖片文字等資源
func setup() {
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red:56/255.0, green:165/255.0, blue:241/255.0, alpha:1)], forState: .Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red:132/255.0, green:132/255.0, blue:132/255.0, alpha:1)], forState: .Normal)
self.tabBar.backgroundImage = UIImage(named: "tabbar_back")
let path = NSBundle.mainBundle().pathForResource("YWTabBarViewController", ofType: "plist")
let array = NSArray(contentsOfFile: path!)
if array != nil {
for dic in array! {
//將類名轉(zhuǎn)化為類
//1.獲取命名空間
let clsName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
let forDic = dic as! NSDictionary
//2.把字符串形式的類名稱轉(zhuǎn)換成類
let classType = NSClassFromString(clsName + "." + "\(forDic["viewController"]!)") as! UIViewController.Type
//3.通過class創(chuàng)建對象
let vc = classType.init()
vc.tabBarItem.image = UIImage(named: "\(forDic["image"]!)")
vc.tabBarItem.selectedImage = UIImage(named: "\(forDic["selectImage"]!)")
vc.tabBarItem.title = "\(forDic["title"]!)"
addChildViewController(vc)
}
}
}