swift 通过plist文件初始化tabbar

使用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! {
                //将类名转化为类
                //1.获取命名空间
                let clsName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
                
                let forDic = dic as! NSDictionary
                //2.把字符串形式的类名称转换成类
                let classType = NSClassFromString(clsName + "." + "\(forDic["viewController"]!)") as! UIViewController.Type
                //3.通过class创建对象
                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)
            }
        }

    }

你可能感兴趣的:(swift 通过plist文件初始化tabbar)