1014 - iOS之UINavigationController类

好啦好啦,终于到UINavigationController,项目都已经快跟不上进度了。

Class

UINavigationController

--A container view controller that defines a stack-based scheme for navigating hierarchical content.

    UINavigationController是一个容器vc,基于栈模式来实现层次结构中内容之间的导航。

        

Declaration

class UINavigationController : UIViewController       //继承于UIViewController

Overview

--A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface.In this type of interface, only one child view controller is visible at a time.

   navigation controller是一个管理导航界面中的多个vc的vc容器。这种导航界面一次只能让一个vc可见。通过屏幕顶部的导航栏的返回按钮返回前一个vc,通过选择vc中的某一项来将新的vc覆盖旧的vc。

--For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.

    除根视图外,导航vc为每一个vc都提供了一个返回按钮,让用户返回视图层的上一层

导航vc的界面看下图:

图片来源苹果官网
图片来源苹果官网

 

--A navigation controller object manages its child view controllers using an ordered array, known as the navigation stack.

   导航vc管理子vc的栈就叫做导航栈

--The user can also remove the topmost view controller using the back button in the navigation bar or using a left-edge swipe gesture.

    客户可以通过顶部导航栏的返回按钮返回上一层视图,也可以通过左边缘手势返回。

--The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface.

     导航vc提供了界面顶部的导航栏,也提供一个可选的屏幕底部的工具栏

--The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers

    导航vc顶部的导航栏是一直存在的, 但是导航vc是通过子vc提供的内容修改顶部导航栏的,就是说导航栏仍是导航vc的,只是借用了子vc的手而已。其实,一般改来改去的都是这个导航栏而已了。

--When the isToolbarHidden property is false, the navigation controller similarly updates the toolbar with contents provided by the topmost view controller.

    当导航vc的isToolbarHidden属性值为false时,导航vc是通过栈顶的vc来更新工具栏的。工具栏是可选的,是屏幕底部的。

--A navigation controller coordinates its behavior with its delegate object.

   导航vc是通过它的委托对象来协调它的行为的。又是委托。

--The delegate object you provide must conform to the UINavigationControllerDelegate protocol.

   导航vc的委托对象必须遵循UINavigationControllerDelegate协议,所以导航vc自身也可以是自己的委托咯,遵循协议咯。

--Figure 2 shows the relationships between the navigation controller and the objects it manages. Use the specified properties of the navigation controller to access these objects.

    下图展示了导航vc与它管理的对象之间的关系。使用这些特定的属性可以访问导航vc管理的对象。

图片来源苹果官网
图片来源苹果官网

 

 

Navigation Controller Views                                                                      -- 导航控制器的视图层次

 

--You access a navigation controller’s view from its view property. This view incorporates the navigation bar, an optional toolbar, and the content view corresponding to the topmost view controller.

   你可以通过导航vc的view属性访问它的view,这个view包含了屏幕顶部的导航栏、屏幕底部的可选的工具栏、以及栈顶vc管理的内容view

--Although the content of the navigation bar and toolbar views changes, the views themselves do not. The only view that actually changes is the custom content view provided by the topmost view controller on the navigation stack.

    导航栏和工具栏视图内容虽然会发生改变是这两个view是一直存在的,不被更改的。唯一被更改的view就是你提供的栈顶vc管理的view。

下图展示了导航vc的view组成。

图片来源苹果官网
图片来源苹果官网

--Because the content view underlaps the navigation bar in iOS 7 and later, you must consider that space when designing your view controller content.

   在iOS 7 之后, 内容视图的范围从导航栏的底部延伸到屏幕的顶部

--It is permissible to customize the navigation bar’s appearance-related properties but you must never change its frame, bounds, or alpha values directly.

   可以自定义导航栏和工具栏外观,但你是不能直接修改它们的边框、范围以及透明度

--If you subclass UINavigationBar, you must initialize your navigation controller using the init(navigationBarClass:toolbarClass:) method.

   如果你使用的是子类化导航vc,那么你必须通过调用子类vc的init(navigationBarClass:toolbarClass:)方法来实例化你的导航vc。

--To hide or show the navigation bar, use the isNavigationBarHidden property or setNavigationBarHidden(_:animated:) method.  

    隐藏或者展示导航栏,你可以使用导航vc中的isNavigationBarHidden属性和setNavigationBarHidden(_:animated:) 方法

--To customize the overall appearance of a navigation bar, use UIAppearance APIs.

    ★  如果你要自定义导航栏的整体外观,请使用UIAppearance接口

--A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack.

   导航vc是通过“导航项”对象来动态配置导航栏的,它是导航vc中的一个属性,与栈顶的子vc关联

--To change the contents of the navigation bar, you must therefore configure the navigation items of your custom view controllers.

   如果你要修改导航栏的内容,你必须在你的当前vc中配置导航栏对象

 

 

Updating the Navigation Bar                                                                                                -- 更新导航栏

 

--Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly.

    每次栈顶vc发生改变的时候,导航vc都会同步更新导航栏

--Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class. You can create items with custom content or create standard system items depending on your needs.

    实际上,导航vc只是更新了导航栏的左、中、右三个位置的导航栏按钮。导航栏按钮是UIBarButtonItem 类的对象,你可以自定义导航栏的内容,也可以使用系统提供的风格。

--Tinting of the navigation bar is controlled by properties of the navigation bar itself. Use the tintColor property to change the tint color of items in the bar and use the barTintColor property to change the tint color of the bar itself.

    导航栏的色调是由导航栏对象自身控制的,导航栏的tintColor属性改变的是导航栏中的组件的色调barTintColor改变的事导航栏的背景颜色

 

 

The Left Item                                                                                                                    --  导航栏的左端项目

 

--For all but the root view controller on the navigation stack, the item on the left side of the navigation bar provides navigation back to the previous view controller. The contents of this left-most button are determined as follows:

   除了导航栈中的根vc外,其他所有的栈中的vc的导航栏的左方都有一个返回项,返回上一个vc。这个最左边的按钮由以下内容决定:

  • If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

       ★★ 如果栈顶vc自定义的导航栏左边按钮,则自定义的左按钮优先于默认的。你可以使用导航vc的leftBarButtonItem属性来指定自定义的左按钮对象。

  • If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has an object in its backBarButtonItem property, the navigation bar displays that item.

       ★★ 如果你没有自定义导航栏左按钮,但是栈的前一个子vc的导航栏对象设置了backBarButtonItem属性,则导航栏使用前一个vc的左侧item作为当前的左侧item

  • If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

       默认的左按钮使用前一个vc的title作为自己的title。

 

 

The Middle Item                                                                                                --  导航栏中间位置的item

 

The navigation controller updates the middle of the navigation bar as follows:

  • If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item.

        如果栈顶vc有自定义的titile view 则导航栏中部项将使用这个tittle view。如果你要自定义导航栏中部视图,你可以使用导航栏的title view属性。

  • If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the title property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the title property of the view controller’s navigation item instead.

       如果没有自定义的tiltle view ,则导航栏中部就使用栈顶vc的默认标题形成一个标签。你可以通过设置栈顶vc的title属性来修改导航栏中部的标题。

 

 

The Right Item  --  导航栏右边项目

 

The navigation controller updates the right side of the navigation bar as follows:

  • If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.

       如果栈顶vc有自定义的右边item对象则会显示自定义的右边item对象。你可以使用导航栏对象的 rightBarButtonItem 属性来设置导航栏右边item对象。

  • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

       如果没有自定义的右边按钮对象,则导航栏右边什么也不展示

 

 

Displaying a Toolbar  --  展示底部工具栏

 

--When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller.

    这个值得注意了,工具栏默认是不显示的,但是你可以通过栈顶vc的属性来设置工具栏,注意,是栈顶vc的,不是导航vc。

--The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden(_:animated:) method of your navigation controller object.

    但是呢,你隐藏工具栏又是调用导航vc中setToolbarHidden(_:animated:) 方法。如果任一个子vc都没有自定义工具栏,那么导航vc的代理对象也是自动调用这个方法隐藏工具栏的。

--To use a custom UIToolbar subclass, initialize the navigation controller using the init(navigationBarClass:toolbarClass:) method.

   如果你使用自定义的工具栏,那么你可以使用导航vc的init(navigationBarClass:toolbarClass:) 初始化方法来实例化导航vc对象。

 

 

Adapting to Different Environments                                                                               --  适应横竖屏环境

 

--When toggling between the two environments, only the size of the navigation controller’s view changes. The navigation controller does not change its view hierarchy or the layout of its views.

   切换水平和垂直屏幕环境的时候,导航vc的视图只改变大小,视图的布局和层次不变。

 

 

Interface Behaviors                                                                                                  --  用户界面的行为

 

用户界面行为,设备水平和垂直屏幕时,导航vc的行为。

--If the navigation controller has a delegate object, the delegate can specify a different set of supported orientations using the navigationControllerSupportedInterfaceOrientations(_:) method.

   你可以用导航vc的委托对象的navigationControllerSupportedInterfaceOrientations(_:)方法来设置导航vc支持的设备方位变化,通过方位集合设置。

--When the modal transition style is UIModalPresentationStyle.currentContext or UIModalPresentationStyle.overCurrentContext, modal presentations from the view controllers in the navigation stack cover the entire navigation interface.

    当导航vc的模态转换属性为上面三种时,模态窗口占满整个屏幕

 

 

State Preservation                                                                                                 -- 保存状态信息                  

 

--The navigation controller starts at the bottom of the stack and moves upward, encoding each view controller that also has a valid restoration identifier string.During the next launch cycle, the navigation controller restores the preserved view controllers to the navigation stack in the same order that they were preserved.

   在保存导航vc的状态时,导航vc是从栈底向上逐个编码带有储存标识符的vc的,这有点违反栈数据结构,但是,在下一个启动周期的时候,在恢复导航vc状态时,也是按照这个编码顺序恢复的。所以你的导航vc和你的子vc都要设置restorationIdentifier属性。如果导航vc和子vc的储存标识符字符串是一样的,导航vc会自动为子vc的标识符添加额外信息,避免冲突。

--For more information about how state preservation and restoration works, see Preserving Your App's UI Across Launches.

   更多储存对象状态的信息,参考超链接。

 

 

Topics                                                                                                                     --专题

Creating Navigation Controllers                                                                                   --创建导航vc

 

init(rootViewController: UIViewController)                                            //便利构造器:参数为容器的根vc

Initializes and returns a newly created navigation controller.

 

init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?)                        //构造器:指定导航栏和工具栏

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

 

init(nibName: String?, bundle: Bundle?)                                       //构造器:通过指定的nib文件构造vc

 

init?(coder: NSCoder)                                                                                   //必要构造器

 

 

Customizing the Navigation Interface Behavior                                            --自定义导航界面的行为

 

var delegate: UINavigationControllerDelegate?                 //属性:设置导航vc的代理对象,可自定义来响应额外的动作方法

The delegate of the navigation controller object.

 

protocol UINavigationControllerDelegate                                                                             //协议:导航vc的代理协议

Use a navigation controller delegate (a custom object that implements this protocol) to modify behavior when a view controller is pushed or popped from the navigation stack of a UINavigationController object.

 

 

Accessing Items on the Navigation Stack                                      --访问导航栈中的子vc

 

var topViewController: UIViewController?                                                              //get属性:栈顶vc

The view controller at the top of the navigation stack.

 

var visibleViewController: UIViewController?                               //get属性:当前可见视图的vc,(栈顶或者模态的vc)

The view controller associated with the currently visible view in the navigation interface.

 

var viewControllers: [UIViewController]                                            //数组属性:该数组即为导航栈,可以访问

The view controllers currently on the navigation stack.

 

func setViewControllers([UIViewController], animated: Bool)                    //方法:设置vc数组为导航栈              

Replaces the view controllers currently managed by the navigation controller with the specified items.

 

 

Pushing and Popping Stack Items                                                                  --推压导航栈

 

func pushViewController(UIViewController, animated: Bool)                             //方法:压栈vc,并更新显示

Pushes a view controller onto the receiver’s stack and updates the display.

 

func popViewController(animated: Bool) -> UIViewController?                         //方法:出栈vc,并更新显示

Pops the top view controller from the navigation stack and updates the display.

 

func popToRootViewController(animated: Bool) -> [UIViewController]?                  //方法:出栈根vc之外的所有vc

Pops all the view controllers on the stack except the root view controller and updates the display.

 

func popToViewController(UIViewController, animated: Bool) -> [UIViewController]?

                                                                                      //方法:出栈到指定的vc元素,返回值是被迫已经出栈了的那些vc数组

Pops view controllers until the specified view controller is at the top of the navigation stack.

 

var interactivePopGestureRecognizer: UIGestureRecognizer?                 //get属性:与出栈有关的手势识别器,可复写

The gesture recognizer responsible for popping the top view controller off the navigation stack.

 

 

Configuring Navigation Bars                                                                                --配置导航栏

 

var navigationBar: UINavigationBar                                                                                      //get属性:导航vc的导航栏

The navigation bar managed by the navigation controller.

 

func setNavigationBarHidden(Bool, animated: Bool)                                         //方法:设置隐藏导航栏

Sets whether the navigation bar is hidden.

 

Customizing Your App’s Navigation Bar                                                                   //★文章和示例:自定义导航栏的外观

Create custom titles, prompts, and buttons in your app’s navigation bar.

 

 

Configuring Custom Toolbars                                                                                --配置自定义工具栏

 

var toolbar: UIToolbar!                                                                                              //get属性:与导航vc绑定的内建工具栏

The custom toolbar associated with the navigation controller.

 

func setToolbarHidden(Bool, animated: Bool)                                                 //方法:设置隐藏内建工具栏

Changes the visibility of the navigation controller’s built-in toolbar.

 

var isToolbarHidden: Bool                                                                                         //属性:设置是否隐藏内建工具栏

A Boolean indicating whether the navigation controller’s built-in toolbar is visible.

 

class let hideShowBarDuration: CGFloat                                         //静态常量属性:工具栏的动画持续时间,是常量,不可改

This variable specifies the duration when animating the navigation bar. Note that this is a constant value, so it cannot be set.

 

 

Hiding the Navigation Bar                                                                                                        --隐藏导航栏

 

var hidesBarsOnTap: Bool                                     //属性:设置是否可以通过点击手势(点击内容时)隐藏导航vc的条栏

A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.

 

var hidesBarsOnSwipe: Bool                                 //属性:设置滑动手势触发时是否隐藏导航vc的条栏,上滑隐藏,下滑显示

A Boolean value indicating whether the navigation bar hides its bars in response to a swipe gesture.

 

var hidesBarsWhenVerticallyCompact: Bool                       //属性:设置垂直紧凑环境下是否隐藏导航vc的条栏

A Boolean value indicating whether the navigation controller hides its bars in a vertically compact environment.

 

var hidesBarsWhenKeyboardAppears: Bool                               //属性:设置键盘出现时是否隐藏导航vc的条栏

A Boolean value indicating whether the navigation controller hides its bars when the keyboard appears.

 

var isNavigationBarHidden: Bool                                                                                 //属性:设置是否隐藏导航栏

A Boolean value that indicates whether the navigation bar is hidden.

 

var barHideOnTapGestureRecognizer: UITapGestureRecognizer            

                             //get属性:触发隐藏导航vc条栏的点击手势识别器,不可复写,只能通过其他方法来操作

The gesture recognizer used to hide and show the navigation and toolbar.

 

var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer

                          //get属性:触发隐藏导航vc条栏的滑动手势识别器,不可复写,只能通过其他方法来操作

The gesture recognizer used to hide the navigation bar and toolbar.

 

 

Action Method for Displaying View Controllers                                        --展示vc的动作方法

 

func show(UIViewController, sender: Any?)                                     //方法:在导航vc中显示参数vc,相当于入栈

Presents the specified view controller in the navigation interface.

 

 

Relationships                                                                                            --关系

 

Inherits From                                                                                          --继承关系

UIViewController                                                    //继承于UIVIewController

 

Conforms To                                                                                           --遵循的协议

 

 

See Also                                                                                           --参考的文章

Container View Controllers                                         --容器vc

 

Creating a Custom Container View Controller                                          //★文章:创建自定义的容器vc

Create a composite interface by combining content from one or more view controllers with other custom views.

 

class UISplitViewController                                                                                                                //类:分割vc类

A container view controller that implements a master-detail interface.

 

class UINavigationBar                                                                                                      //类:导航栏控件的类

Navigational controls displayed in a bar along the top of the screen, usually in conjunction with a navigation controller.

 

class UINavigationItem                                                                                            //类:导航栏控件的某item的类

The items to be displayed by a navigation bar when the associated view controller is visible.

 

class UITabBarController                                                                         //类:标签栏vc的类

A container view controller that manages a radio-style selection interface, where the selection determines which child view controller to display.

 

class UITabBar                                                                                                                        //类:标签栏控件的类

A control that displays one or more buttons in a tab bar for selecting between different subtasks, views, or modes in an app.

 

class UITabBarItem                                                                                     //类:标签栏控件的某item的类

An item in a tab bar.

 

class UIPageViewController                                                                             //类:分页vc的类

A container view controller that manages navigation between pages of content, where each page is managed by a child view controller.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值