美文网首页
swift 通过plist文件初始化tabbar

swift 通过plist文件初始化tabbar

作者: iOS_开发 | 来源:发表于2017-06-11 20:14 被阅读0次

    使用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

          本文链接:https://www.haomeiwen.com/subject/xlqvzttx.html