zoukankan      html  css  js  c++  java
  • 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 的, 所以里面的方法以及属性都是可以使用的.


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

  • 相关阅读:
    C++学习9 this指针详解
    福建省第八届 Triangles
    UVA 11584 Partitioning by Palindromes
    POJ 2752 Seek the Name, Seek the Fame
    UVA 11437 Triangle Fun
    UVA 11488 Hyper Prefix Sets (字典树)
    HDU 2988 Dark roads(kruskal模板题)
    HDU 1385 Minimum Transport Cost
    HDU 2112 HDU Today
    HDU 1548 A strange lift(最短路&&bfs)
  • 原文地址:https://www.cnblogs.com/iOSCain/p/4529340.html
Copyright © 2011-2022 走看看