第一講開始!
萬事開頭難,項目結(jié)構(gòu)框架搭建是整個APP開發(fā)的頭,是核心,是地基,只有結(jié)構(gòu)搭建完成才能繼續(xù)后面的工作,所以這第一講毫無疑問必須要介紹項目的搭建,當然本文僅是基礎(chǔ)搭建,正如標題所示是對導(dǎo)航欄UITabbarController及標簽欄UINavigationController的創(chuàng)建,雖然基礎(chǔ)但也非常重要.
下面??為代碼詳情
首先,看一下項目的目錄結(jié)構(gòu)

首先創(chuàng)建文件MainViewController繼承UITabbarController, MyNavViewController繼承UINavigationController,MyBaseViewController繼承UIViewController,還要創(chuàng)建HomePageViewController, MessageViewController, DiscoverViewController, ProfileViewController都繼承
接下來,在AppDelegate中創(chuàng)建window并設(shè)置跟視圖
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//設(shè)置全局顏色
UITabBar.appearance().tintColor = UIColor.black
//創(chuàng)建 window
window = UIWindow(frame: UIScreen.main.bounds)
//設(shè)置跟控制器為MainViewController
window?.rootViewController = MainViewController()
window?.makeKeyAndVisible()
return true
}
}
然后,一兩種方案創(chuàng)建Tabbar,代碼中有詳細的介紹
import UIKit
class MainViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//查看方案1效果
// setupChildController()
//查看方案2效果
setupChildViewCotroller()
}
//portrait 豎屏
//landscape 橫屏
//多數(shù)情況下我們使用的是豎屏 播放視頻時可用modal實現(xiàn)
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
return.portrait
}
//MARK:方案1
//設(shè)置所有控制器
func setupChildController() {
addChildViewController(HomePageViewController(), title: "首頁", imageName: "tabbar_home")
addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_home")
addChildViewController(DiscoverViewController(), title: "發(fā)現(xiàn)", imageName: "tabbar_home")
addChildViewController(ProfileViewController(), title: "我的", imageName: "tabbar_home")
}
//重載下列方法
/** 方法的重載:方法名稱相同,但是參數(shù)不同. --> 1.參數(shù)的類型不同 2.參數(shù)的個數(shù)不同
private在當前文件中可以訪問,但是其他文件不能訪問
*/
private func addChildViewController(_ childController: UIViewController,title : String, imageName : String) {
// 1.設(shè)置子控制器的屬性
childController.title = title
childController.tabBarItem.image = UIImage(named: imageName)
childController.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
//2.設(shè)置導(dǎo)航欄控制器
let childNaVc = MyNavViewController(rootViewController: childController)
addChildViewController(childNaVc)
}
//MARK:方案2
func setupChildViewCotroller() {
let array = [
["clsName":"HomePageViewController","title":"首頁","imageName":"home"],
["clsName":"MessageViewController","title":"消息","imageName":"home"],
["clsName":"DiscoverViewController","title":"發(fā)現(xiàn)","imageName":"home"],
["clsName":"ProfileViewController","title":"我的","imageName":"home"]
]
var arrayM = [UIViewController]()
for dict in array {
arrayM.append(controllers(dict: dict))
}
viewControllers = arrayM
}
/// 使用字典創(chuàng)建一個子控制器
///
/// - parameter dict: 信息字典[clsName,title,imageName]
/// - returns 子控制器
private func controllers(dict:[String:String])->UIViewController{
//1.取得字典內(nèi)容
//獲取命名空間
let bundleName = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""
/**
let cls = NSClassFromString(bundleName + "." + clsName) as? UIViewController.Type
根據(jù)字符串獲取對應(yīng)的Class并將對應(yīng)的AnyObject轉(zhuǎn)成控制器的類型
*/
guard let clsName = dict["clsName"],let title = dict["title"],let imageName = dict["imageName"],let cls = NSClassFromString(bundleName + "." + clsName) as? UIViewController.Type else {
return UIViewController()
}
//2.創(chuàng)建視圖控制器
let vc = cls.init()
vc.title = title
let nav = MyNavViewController(rootViewController: vc)
//3.設(shè)置tabbar 圖像
vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName)
//withRenderingMode(.alwaysOriginal)顏色渲染
vc.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imageName+"_highlighted")?.withRenderingMode(.alwaysOriginal)
//4.設(shè)置tabbar的標題字體顏色(如果不設(shè)置圖片字體顏色無效果)
vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
return nav
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
接著我們在MyBaseViewController中做一些設(shè)置,項目中很多控制器都是繼承這個BaseViewControlle,以繼承其內(nèi)部的一些共用的屬性或方法(本文中為自定義NavigationBar)
代碼如下??
import UIKit
class MyBaseViewController: UIViewController {
//自定義導(dǎo)航條
lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
//自定義導(dǎo)航條目 - 之后設(shè)置導(dǎo)航條內(nèi)容 統(tǒng)一用 navItem
lazy var navItem = UINavigationItem()
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar()
// Do any additional setup after loading the view.
}
//重寫title的didSet方法
override var title: String?{
didSet{
navItem.title = title
}
}
//設(shè)置導(dǎo)航條
private func setupNavigationBar(){
//添加導(dǎo)航條
view.addSubview(navigationBar)
//將item條目設(shè)置到Bar上
navigationBar.items = [navItem]
//設(shè)置navBar的渲染顏色
navigationBar.barTintColor = UIColor.orange
//設(shè)置字體的顏色
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
//設(shè)置系統(tǒng)按鈕文字的渲染顏色
navigationBar.tintColor = UIColor.white
}
}
最后是對NavigationItem的處理,在MyNavViewController中
import UIKit
class MyNavViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
//隱藏navgationBar(如果需要要重寫navigationBar)
// navigationBar.isHidden = true
// Do any additional setup after loading the view.
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
//如果不是棧底控制器才會隱藏
if childViewControllers.count>0 {
viewController.hidesBottomBarWhenPushed = true
//判斷控制器的級數(shù)
if childViewControllers.count == 1 {
//級數(shù)為1時顯示的是首頁的標題否則顯示 "返回"
title = childViewControllers.first?.title ?? "返回"
}else{
title = "返回"
}
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, style: .done, target: self, action: #selector(popback))
}
super.pushViewController(viewController, animated: true)
}
//返回上一級
@objc private func popback(){
popViewController(animated: true)
}
}
基本的結(jié)構(gòu)框架搭建到這里就結(jié)束,相信對讀者朋友會有所幫助,如果覺得文章還可以,點擊下方??喜歡鼓勵一下,沒關(guān)注的朋友可以點擊一下關(guān)注,專題系列講解持續(xù)更新中!
專題目錄:Swift開發(fā)之功能模塊