(Swift)基礎(chǔ)項目搭建(導(dǎo)航欄UITabbarController及標簽欄UINavigationController的創(chuàng)建)

第一講開始!

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


MyProject.png

首先創(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ā)之功能模塊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,309評論 4 61
  • 談到直覺,不由得想起喬布斯說過,不要陷入教條之中,不要在意別人的想法,不要讓他人蕪雜的意見淹沒你內(nèi)心的聲音。最重要...
    秋秋絮語閱讀 231評論 0 1
  • 人總是幻想著一種彼岸,它可以是過去,也可以是未來的某種狀態(tài),總之不是當下。彼岸總是好的,可以給人一種幻覺,似乎當下...
    Anna亞男閱讀 518評論 7 3
  • 當然,好關(guān)系,要建立在價值觀吻合的前提下。 一個員工進來前,要看他與公司的文化和價值觀是否匹配。如果不匹配,這個人...
    一米的樹洞閱讀 242評論 0 1

友情鏈接更多精彩內(nèi)容