// 各个主页面类 // XQWBHomeViewController.swift // XQWBDiscoverViewController.swift // XQWBMessageViewController.swift // XQWBMineViewController.swift // XQWBBaseViewController.swift // 根据须要建立基类 // XQWBMainViewController.swift (继承 UITabBarController) // XQWBBaseViewController.swift (继承 ViewController) // XQWBNavigationController.swift (继承 NavigationController)
window = UIWindow() window?.backgroundColor = UIColor.white window?.rootViewController = XQWBMainViewController() window?.makeKeyAndVisible()
5.1 声明一个方法,用来建立控制器信息的字典数组,而且经过遍历数组建立控制器,给UITabBarController的viewControllers赋值git
// 设置全部字控制器 func setupChildControllers() { let array = [ ["clsName":"XQWBHomeViewController", "title":"首页", "imgName":"home"], ["clsName":"XQWBDiscoverViewController", "title":"发现", "imgName":"discover"], ["clsName":"UIViewController", "title":"", "imgName":""], // 第二个控制器使用UIViewcontroller 实现占位做用 ["clsName":"XQWBMessageViewController", "title":"消息", "imgName":"message_center"], ["clsName":"XQWBMineViewController", "title":"个人", "imgName":"profile"] ] var arrayM = [UIViewController]() for dict in array { // 注意: swift 3.0之后循环只能使用for_in 来实现,普通的for循环由于 i++的语法被废除而没法使用 arrayM.append(controller(dict: dict)) } viewControllers = arrayM }
5.2 根据控制器信息的字典和反射原理使用字符串建立UIViewController,建立NavigationController,而且返回swift
// 根据字典建立一个子控制器 func controller(dict:[String:String]) -> UIViewController { // 1. 取字典信息 // 这里使用guard let 守护一下,能够避免去到空值,另外能够设置中间的站位UIViewcontroller guard let clsName = dict["clsName"], let title = dict["title"], let imgname = dict["imgName"], let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? UIViewController.Type else { return UIViewController() } // 2. 建立视图控制器 let vc = cls.init() // 2.1 设置标题 vc.title = title; // 2.2 设置图像 vc.tabBarItem.image = UIImage(named: "tabbar_" + imgname); vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imgname + "_highlighted")?.withRenderingMode(.alwaysOriginal) // 3. 设置nav let nav = XQWBNavigationController(rootViewController: vc) // 4. 设置tabbar 字体 vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted) // 设置选择状态下的图片颜色 vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: []) // 设置文字大小只能在normal 状态下设置 return nav }
6.1 声明一个懒加载的属性按钮数组
/// 编写按钮 lazy var composeBtn:UIButton = { // 设置按钮图片 let btn = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button") // 设置按钮点击事件 btn?.addTarget(self, action: #selector(composeStatus), for: .touchUpInside) return btn!; }()
6.2 设置"写微博"按钮的frame架构
/// 设置写按钮 func setupComposeBtn() { // 添加到视图 tabBar.addSubview(composeBtn) // 设置按钮位置 let count = CGFloat(childViewControllers.count) let width = tabBar.bounds.width / count - 1; // 这里-1 是让按钮变宽,防止点击到默认的占位按钮,跳转到占位Viewcontroller composeBtn.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0) }
6.3 实现"写微博"按钮的点击事件app
// FIXME: 写微博没有实现 @objc func composeStatus() { print("写微博") }
7.1 在 BaseViewController 中添加懒加载的UINavigationBar和UINavigationItemdom
// 自定义导航条 lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64)) // 自定义导航项 之后设置导航栏内容 用navItem lazy var navItem = UINavigationItem()
7.2 添加 setupUI() 方法,设置基本UI,可让子类重写ide
func setupUI() { // 随机背景颜色 view.backgroundColor = UIColor.cz_random() // 添加导航条 view.addSubview(navigationBar) // 设置item navigationBar.items = [navItem] // 设置navbar 的渲染颜色 navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6) // 设置navbar title 字体颜色 navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray] }
7.3 重写 title 的disSet 方法,设置navItem.title测试
override var title: String? { didSet { navItem.title = title } }
7.4 在HomeViewController和DemoViewController中重写setupUI()方法,设置BarButtonItem测试push/pop字体
// 重写 setupUI() override func setupUI() { super.setupUI() navItem.rightBarButtonItem = UIBarButtonItem(title: "下一个", target: self, action: #selector(showNext)) } // 实现点击按钮方法 @objc func showNext() { let vc = XQWBDemoViewController() navigationController?.pushViewController(vc, animated: true) }
7.5 重写func pushViewController(_ viewController: UIViewController, animated: Bool) {} 方法,设置当push的时候隐藏tabBar,设置返回按钮显示的文字ui
override func pushViewController(_ viewController: UIViewController, animated: Bool) { if childViewControllers.count > 0 { // 设置push后隐藏tabbar viewController.hidesBottomBarWhenPushed = true // 判断控制器类型 if let vc = viewController as? XQWBBaseViewController { var title = "返回" if childViewControllers.count == 1 { title = childViewControllers.first?.title ?? "返回" } vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBackButton: true) } } super.pushViewController(viewController, animated: animated) }
次日的内容主要自定义了UItabBarController 和 UINavigationBar, 而且处理了push隐藏tabbar和返回按钮的文字