一.修改项目的启动过程
- 将Main Interface处的main删除
- 在application:didFinishLaunchingWithOptions:launchOptions:方法中创建window,并且设置根控制器
// 设置整体主题TabBar的tintColor
UITabBar.appearance().tintColor = UIColor.orangeColor()
// 1.创建window
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
// 2.设置window的根控制器
self.window?.rootViewController = MainViewController()
// 3.让窗口生效
self.window?.makeKeyAndVisible()
- 在MainViewController中添加子控制器
override func viewDidLoad() {
super.viewDidLoad()
// 添加自控制器
self.addChildViewController(HomeViewController(), imageName: "tabbar_home", title: "主页")
self.addChildViewController(MessageViewController(), imageName: "tabbar_message_center", title: "消息")
self.addChildViewController(DiscoverViewController(), imageName: "tabbar_discover", title: "广场")
self.addChildViewController(ProfileViewController(), imageName: "tabbar_profile", title: "我")
}
private func addChildViewController(childCVc: UIViewController, imageName : String, title : String) {
// 1.创建自控制器
let homeNav = UINavigationController(rootViewController: childCVc)
// 2.设置标题
childCVc.title = title
childCVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
childCVc.tabBarItem.image = UIImage(named: imageName)
// 3.添加到UITabbarController
self.addChildViewController(homeNav)
}