UIKit框架-高級控件Swift版本: 9.UINavigationController方法/屬性詳解

前面咱們講解了UISegemtedControl分段式控件, 如今讓咱們來看看 iOS 另外一個很是經常使用的控件, UINavigationController.css


1.UINavigationController經常使用屬性

// 1.獲取 UINavigationController 的頂部的視圖控制器
    var topViewController: UIViewController! { get }

// 2.獲取 UINavigationController 可見的視圖控制器
    var visibleViewController: UIViewController! { get }

// 3.設置 UINavigationController 的 viewControllers 對象
    var viewControllers: [AnyObject]!

// 4.設置 UINavigationController 的導航欄控制器是否隱藏, 默認是 false
    var navigationBarHidden: Bool

// 5.獲取 UINavigationController 的導航欄控制器
    var navigationBar: UINavigationBar { get }

// 6.設置 UINavigationController 的內置工具欄是否可見(默認是 ture)
    var toolbarHidden: Bool

// 7.獲取 UINavigationController 的 toolbar
    var toolbar: UIToolbar! { get }

// 8.設置 UINavigationController 的代理對象
    var delegate: UINavigationControllerDelegate?

// 9.獲取 UINavigationController 的手勢識別頂部視圖控制器
    var interactivePopGestureRecognizer: UIGestureRecognizer! { get }

// 10.設置 UINavigationController 當鍵盤出現時是否隱藏導航欄和工具欄
    var hidesBarsWhenKeyboardAppears: Bool

// 11.設置 UINavigationController 是否使用向上滑動的手勢隱藏導航欄和工具欄
    var hidesBarsOnSwipe: Bool

// 12.獲取 UINavigationController 用手勢識別隱藏導航欄和工具欄
    var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer { get }

// 13.設置 UINavigationController 是否在垂直顯示時隱藏
    var hidesBarsWhenVerticallyCompact: Bool

// 14.設置 UINavigationController 是否使用點擊手勢來隱藏
    var hidesBarsOnTap: Bool

// 15.獲取 UINavigationController 隱藏時所使用的手勢
    var barHideOnTapGestureRecognizer: UITapGestureRecognizer { get }

2.UINavigationController經常使用的方法

// 1.該方法是用來設置 UINavigationController 跳轉到指定的視圖控制器, 是否使用動畫.
    func pushViewController(viewController: UIViewController, animated: Bool)

// 2.該方法是用來設置 UINavigationController Pop到其餘視圖控制器時是否使用動畫, 而且返回的類型必須是 UIViewController
    func popViewControllerAnimated(animated: Bool) -> UIViewController?

// 3.該方法是用來設置 UINavigationController Pop到指定的視圖控制器, 是否使用動畫, 返回的類型是任意類型
    func popToViewController(viewController: UIViewController, animated: Bool) -> [AnyObject]?

// 4.該方法是用來設置 UINavigationController Pop到根視圖時是否使用動畫, 而且返回的類型必須是任意類型
    func popToRootViewControllerAnimated(animated: Bool) -> [AnyObject]?

// 5.該方法是用來替換以前於 UINavigationController 綁定的視圖控制器, 而且是否使用動畫
    func setViewControllers(viewControllers: [AnyObject]!, animated: Bool)

// 6.該方法是用來設置 UINavigationController 的導航欄是否隱藏, 是否使用動畫
    func setNavigationBarHidden(hidden: Bool, animated: Bool)

// 7.該方法是用來設置 UINavigationController 的工具欄是否隱藏, 是否使用動畫
    func setToolbarHidden(hidden: Bool, animated: Bool)

// 8.該方法是用來設置 UINavigationController 顯示指定的 ViewController
    func showViewController(vc: UIViewController, sender: AnyObject!)

3.UINavigationController代理方法

// 1.該方法使用來設置 UINavigationController 將要顯示時所調用的方法
    optional func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)

 // 2.該方法使用來設置 UINavigationController 徹底顯示時所調用的方法
    optional func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool)

4.代碼演示

首先咱們要再AppDelegate.swift文件中實現swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor = UIColor.grayColor()
        self.window!.makeKeyAndVisible()

        let viewController = ViewController()
        let navigationController = UINavigationController(rootViewController: viewController)
        self.window!.rootViewController = navigationController

        return true
    }

遵照代理協議ruby

class ViewController: UIViewController, UINavigationControllerDelegate { }

自定義UINavigationControllermarkdown

func myNavigationContronller() {
        // 1.設置 UINavigationController 的 Title
        self.title = "UINavigationContronller"

        // 2.設置 UIVavigationController 的按鈕 Title, Style, Target, Action 等方法屬性
        let backBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
        let nextBarButtonItem = UIBarButtonItem(title: "下一頁", style: UIBarButtonItemStyle.Plain, target: self, action: "nextAction")

        // 3.設置 UINavigationItem
        self.navigationItem.leftBarButtonItem = backBarButtonItem
        self.navigationItem.rightBarButtonItem = nextBarButtonItem

        // 4.獲取 UINavigationController 的頂部的視圖控制器
        let topView = self.navigationController?.topViewController
        println(topView)

        // 5.獲取 UINavigationController 可見的視圖控制器
        let visibleView = self.navigationController?.visibleViewController
        println(visibleView)

        // 6.設置 UINavigationController 的導航欄控制器
        self.navigationController?.viewControllers

        // 7.設置 UINavigationController 的導航欄控制器是否隱藏(默認是 false)
        self.navigationController?.navigationBarHidden = false

        // 8.獲取 UINavigationController 的導航欄控制器
        let navigationBar = self.navigationController?.navigationBar
        println(navigationBar)

        // 9.設置 UINavigationController 的內置工具欄是否可見(默認是 ture)
        self.navigationController?.toolbarHidden = false

        // 10.獲取 UINavigationController 的 toolbar
        let toolbar = self.navigationController?.toolbar
        println(toolbar)

        // 11.設置 UINavigationController 的代理對象
        self.navigationController?.delegate = self

        // 12.獲取 UINavigationController 的手勢識別頂部視圖控制器
        let pop = self.navigationController?.interactivePopGestureRecognizer
        println(pop)

        // 13.設置 UINavigationController 當鍵盤出現時是否隱藏導航欄和工具欄
        self.navigationController!.hidesBarsWhenKeyboardAppears = true

        // 14.設置 UINavigationController 是否使用向上滑動的手勢隱藏導航欄和工具欄
        self.navigationController?.hidesBarsOnSwipe = true

        // 15.獲取 UINavigationController 用手勢識別隱藏導航欄和工具欄
        let barHide = self.navigationController!.barHideOnSwipeGestureRecognizer
        println(barHide)

        // 16.設置 UINavigationController 是否在垂直顯示時隱藏
        self.navigationController!.hidesBarsWhenVerticallyCompact = true

        // 17.設置 UINavigationController 是否使用點擊手勢來隱藏
        self.navigationController?.hidesBarsOnTap = true

        // 18.獲取 UINavigationController 隱藏時所使用的手勢
        let barHideOnTap = self.navigationController!.barHideOnTapGestureRecognizer
        println(barHideOnTap)

        // 19.設置 UINavigationController 的導航欄是否隱藏, 是否使用動畫
        self.navigationController?.setNavigationBarHidden(true, animated: true)

        // 20.設置 UINavigationController 的工具欄是否隱藏, 是否使用動畫
        self.navigationController?.setToolbarHidden(true, animated: true)
    }

自定義代理方法以及監聽方法app

// 1.該方法使用來設置 UINavigationController 將要顯示時所調用的方法
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        println("UINavigationController 將要顯示")
    }

    // 2.該方法使用來設置 UINavigationController 徹底顯示時所調用的方法
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        println("UINavigationController 徹底顯示")
    }

    // 3.返回按鈕的監聽方法
    func backAction() {
        println("點擊了返回")
    }

    // 4.下一頁按鈕的監聽方法
    func nextAction() {
        println("點擊了下一頁")
    }

5.最終效果

1

2

PS: UINavigationController 是繼承與 UIViewController 的, 因此裏面的方法以及屬性都是能夠使用的.ide


好了, 此次咱們就講到這裏, 下次咱們繼續~~工具

相關文章
相關標籤/搜索