UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解

前面我们讲解了UISegemtedControl分段式控件, 现在让我们来看看 iOS 另一个非常常用的控件, UINavigationController.


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文件中实现

    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
    }

遵守代理协议

class ViewController: UIViewController, UINavigationControllerDelegate { }

自定义UINavigationController

    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)
    }

自定义代理方法以及监听方法

    // 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 的, 所以里面的方法以及属性都是可以使用的.


好了, 这次我们就讲到这里, 下次我们继续~~

转载于:https://www.cnblogs.com/iOSCain/p/4529340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值