步骤
- 获取JSON文件路径
- 根据路径创建NSData对象
- 根据NSData对象对JSON进行序列化,得到一个数组
- 遍历数组,提取出
视图名
,标题
,图片名
,根据这三项创建视图,并用UINavigationViewController包裹起来,将navi添加到tabBarViewController中.
MainVCSettings.json文件
[
{
"vcName": "HomeTableViewController",
"title": "首页",
"imageName": "tabbar_home"
},
{
"vcName": "MessageTableViewController",
"title": "消息",
"imageName": "tabbar_message_center"
},
{
"vcName": "NullViewController",
"title": "",
"imageName": ""
},
{
"vcName": "DiscoverTableViewController",
"title": "广场",
"imageName": "tabbar_discover"
},
{
"vcName": "ProfileTableViewController",
"title": "我",
"imageName": "tabbar_profile"
}
]
具体实现代码
//
// MainViewController.swift
// weibo
//
// Created by Adrift on 16/6/5.
// Copyright © 2016年 Adrift. All rights reserved.
//
import UIKit
class MainViewController: UITabBarController {
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
//设置当前控制器对应的tabBar的颜色,iOS7之前如果设置了tintClor只有文字会变,而图片不会变
tabBar.tintColor = UIColor.orangeColor()
addChildViewControllers()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
tabBar.addSubview(composeBtn)
//计算宽度
let width = UIScreen.mainScreen().bounds.width / CGFloat(viewControllers!.count)
//创建 frame
let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
//设置 frame 跟 偏移量
composeBtn.frame = CGRectOffset(rect, width * 2, 0)
}
//MARK: - 方法
/**
添加所有子控制器
*/
func addChildViewControllers() {
//获取json文件路径
let path = NSBundle.mainBundle().pathForResource("MainVCSettings", ofType: "json")
//通过文件路径创建NSData
if let tempPath = path {
let jsonData = NSData(contentsOfFile: tempPath)
do {
//序列化
let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! [[String: String]]
//遍历数组,并创建视图控制器
for dict in dictArr {
addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
}
}catch{
print("crash")
//并添加到TabBarViewController
addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
addChildViewController("NullViewController", title: "", imageName: "")
addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
addChildViewController("ProfileTableViewController", title: "我的", imageName: "tabbar_profile")
}
}
}
/**
将navigationViewController 添加到 TabBarViewController
- parameter childController: 子控制图
- parameter title: 标题
- parameter imageName: 非高亮图片名称
*/
private func addChildViewController(childControllerName: String, title: String, imageName: String) {
//获取命名空间
let speaceName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
//通过字符串转换为类
let cls:AnyClass? = NSClassFromString(speaceName + "." + childControllerName)
//通过类创建对象
let vcCls = cls as! UIViewController.Type
let vc = vcCls.init()
// 设置首页
vc.tabBarItem.image = UIImage(named: imageName)
vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
vc.title = title
//2.给首页包装一个导航器
let navi = UINavigationController()
navi.addChildViewController(vc)
//3.将导航控制器添加到当前控制器
addChildViewController(navi)
}
func composeBtnClick() {
print("被点击...")
}
//MARK: - 懒加载
private lazy var composeBtn:UIButton = {
let button = UIButton()
//设置button图片
button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
//设置button背景图片
button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//添加监听事件
button.addTarget(self, action: #selector(MainViewController.composeBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
}
–EOF–