Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

前面我们讲解了UISegemtedControl分段式控件, 现在让我们来看看 iOS 另一个非常常用的控件, UINavigationController. 1.UINavigationController常用属性 // 1.获取 UINavigationController 的顶部的视图控制器 var topViewController: UIViewController! { get
@H_696_14@

前面我们讲解了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: "下一页",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.最终效果

PS: UINavigationController 是继承与 UIViewController 的,所以里面的方法以及属性都是可以使用的.

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

大佬总结

以上是大佬教程为你收集整理的UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解全部内容,希望文章能够帮你解决UIKit框架-高级控件Swift版本: 9.UINavigationController方法/属性详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。