首先記得在AppDelegate指定rootViewController
window = UIWindow (frame: UIScreen.main.bounds)
window?.rootViewController = TabbarController()
window?.makeKeyAndVisible()
然后在TabbarController里寫入下面兩個(gè)方法并使用就好
private func addChildViewController() {
addChildViewController(controller: HomeViewController(), title: "首頁(yè)", imageName: "tabbar_home")
addChildViewController(controller: MessageViewController(), title: "消息", imageName: "tabbar_message")
addChildViewController(controller: DiscoverViewController(), title: "發(fā)現(xiàn)", imageName: "tabbar_discover")
addChildViewController(controller: MeViewController(), title: "我", imageName: "tabbar_me")
}
private func addChildViewController(controller:UIViewController, title:String,imageName:String) {
controller.tabBarItem.image = UIImage(named: imageName)
controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
controller.tabBarItem.title = title
let nav = NavigationController()
nav.addChildViewController(controller)
addChildViewController(nav)
}
創(chuàng)建NavigationController充尉,在這里我們可以對(duì)導(dǎo)航欄做一些默認(rèn)配置钮蛛,比如字體论熙,顏色趁蕊,大小什么的,而且在這里去自定義左側(cè)返回按鈕比較方便
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.interactivePopGestureRecognizer!.delegate = nil;
let appearance = UINavigationBar.appearance()
appearance.isTranslucent = false
appearance.setBackgroundImage(UIImage.imageWithColor(color: Color_NavBackground, size: CGSize(width: 1, height: 1)), for: UIBarMetrics.default)
var textAttrs:[String:AnyObject] = Dictionary()
textAttrs[NSForegroundColorAttributeName] = UIColor.white
textAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 16)
appearance.titleTextAttributes = textAttrs
}
這樣一個(gè)項(xiàng)目的基本框架就算搭建完成了,四個(gè)主頁(yè)面建議繼承一個(gè)BaseViewController煮纵,方便我們做一些公共部署机错。