iOS UINavigationController

来源:互联网 发布:软件打不开闪退怎么办 编辑:程序博客网 时间:2024/05/19 09:39

UINavigation​Controller

The UINavigation​Controller class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but you may also subclass to customize the class behavior.

  • Swift
  • Objective-C
  • iOS 2.0+
  • tvOS 2.0+
  • Overview
  • Symbols
  • Relationships

Overview

The screens presented by a navigation interface typically mimic the hierarchical organization of your data. At each level of the hierarchy, you provide an appropriate screen (managed by a custom view controller) to display the content at that level. Figure 1 shows an example of the navigation interface presented by the Settings application in iOS Simulator. The first screen presents the user with the list of applications that contain preferences. Selecting an application reveals individual settings and groups of settings for that application. Selecting a group yields more settings and so on. For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.

Figure 1 

A sample navigation interface

A sample navigation interface

A navigation controller object manages the currently displayed screens using the navigation stack, which is represented by an array of view controllers. The first view controller in the array is the root view controller. The last view controller in the array is the view controller currently being displayed. You add and remove view controllers from the stack using segues or using the methods of this class. 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. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by the view controllers on the navigation stack. When the is​Toolbar​Hidden property is false, the navigation controller similarly updates the toolbar with contents provided by the topmost view controller.

A navigation controller coordinates its behavior with its delegate object. The delegate object can override the pushing or popping of a view controller, provide custom animation transitions, and specify the preferred orientation for the navigation interface. The delegate object you provide must conform to the UINavigation​Controller​Delegate protocol.

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.

Figure 2 

Objects managed by the navigation controller

Navigation Controller Views

A navigation controller is a container view controller—that is, it embeds the content of other view controllers inside of itself. You access a navigation controller’s view from its viewproperty. This view incorporates the navigation bar, an optional toolbar, and the content view corresponding to the topmost view controller. Figure 3 shows how these views are assembled to present the overall navigation interface. (In this figure, the navigation interface is further embedded inside a tab bar interface.) 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.

Figure 3 

The views of a navigation controller

The views of a navigation controller

Note

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

The navigation controller manages the creation, configuration, and display of the navigation bar and optional navigation toolbar. It is permissible to customize the navigation bar’s appearance-related properties but you must never change its framebounds, or alpha values directly. If you subclass UINavigation​Bar, you must initialize your navigation controller using theinit(navigation​Bar​Class:​toolbar​Class:​) method. To hide or show the navigation bar, use the is​Navigation​Bar​Hidden property or set​Navigation​Bar​Hidden(_:​animated:​) method.

A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigation​Item class) associated with the view controllers on the navigation stack. To customize the overall appearance of a navigation bar, use UIAppearance APIs. To change the contents of the navigation bar, you must therefore configure the navigation items of your custom view controllers. For more information about navigation items, see UINavigation​Item.

Updating the Navigation Bar

Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly. 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 UIBar​Button​Item class. You can create items with custom content or create standard system items depending on your needs.

Tinting of the navigation bar is controlled by properties of the navigation bar itself. Use thetint​Color property to change the tint color of items in the bar and use the bar​Tint​Colorproperty to change the tint color of the bar itself. Navigation bars do not inherit their tint color from the currently displayed view controller.

For more information about the navigation bar, see UINavigation​Bar. For more information about how to create bar button items, see UIBar​Button​Item.

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:

  • 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 left​Bar​Button​Itemproperty of the view controller’s navigation item.

  • 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 back​Bar​Button​Itemproperty, the navigation bar displays that 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.)

Note

In cases where the title of a back button is too long to fit in the available space, the navigation bar may substitute the string “Back” for the actual button title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the left​Bar​Button​Item or left​Bar​Button​Items property of its navigation item—the navigation bar does not change the button title.

The Middle 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 title​Viewproperty of the view controller’s navigation item.

  • 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 titleproperty 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.

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 right​Bar​Button​Itemproperty of the view controller’s navigation 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

A navigation controller object manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbar​Items property of the active view controller. When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the set​Toolbar​Hidden(_:​animated:​) method of your navigation controller object. If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations. To use a custom UIToolbar subclass, initialize the navigation controller using theinit(navigation​Bar​Class:​toolbar​Class:​) method. If you use custom toolbar and navigation bar subclasses to create a navigation controller, note that you are responsible for pushing and setting view controllers before presenting the navigation controller onscreen.

Adapting to Different Environments

The navigation interface remains the same in both horizontally compact and horizontally regular 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.

When configuring segues between view controllers on a navigation stack, the standard Show and Show Detail segues behave as follows:

  • Show segue—The navigation controller pushes the specified view controller onto its navigation stack.

  • Show Detail segue—The navigation controller presents the specified view controller modally.

The behaviors of other segue types are unchanged.

Interface Behaviors

A navigation controller supports the following behaviors for its interface:

  • Supported interface orientations—A navigation controller object does not consult the view controllers on its navigation stack when determining the supported interface orientations. On iPhone, a navigation controller supports all orientations except portrait upside-down. On iPad, a navigation controller supports all orientations. If the navigation controller has a delegate object, the delegate can specify a different set of supported orientations using the navigation​Controller​Supported​Interface​Orientations(_:​) method.

  • Presentation context—A navigation controller defines the presentation context for modally presented view controllers. When the modal transition style is current​Contextor over​Current​Context, modal presentations from the view controllers in the navigation stack cover the entire navigation interface.

State Preservation

In iOS 6 and later, if you assign a value to this view controller’s restoration​Identifierproperty, it attempts to preserve the child view controllers on its navigation stack. 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.

The child view controllers you push onto the navigation stack may use the same restoration identifiers. The navigation controller automatically stores additional information to ensure that each child’s restoration path is unique.

For more information about how state preservation and restoration works, see App Programming Guide for iOS.



1、 UINavigationController 是一个容器类。里面盛放的是UIViewController。

容器的意思是,如果你不放入UIViewController,里面就是空的,什么也没有。

这个容器在管理UIViewController时,遵循栈管理的原则(后进先出)。

 UIViewController的入栈操作

UIViewController *aViewController = [[UIView alloc] init;[self.navigationController pushViewController:aViewController   animated:NO];

出栈操作

[self.navigationController popViewControllerAnimated:YES]; 

2、UINavigationController虽然是个容器,但也不是干巴巴的空壳。它由以下四部分组成:Navigation toolbar、Custom content、Navigation bar、,Navigation view。其中,我猜测,入栈的UIViewController 其界面应该就展示在Custom content区域。

参考:http://hi.baidu.com/iphone8/item/f3b3cb6f00cded2e68105b21

UINavigationItem部分

另外据悉,UINavigationController会为每一个入栈的UIViewController生成一个UINavigationItem. UIViewController通过修改UINavigationItem可以控制UINavigationBar上的按钮和标题等。如下:

你可以通过设置self.navigationItem.leftBarButtonItem为某个ButtonItem,

self.navigationItem.leftBarButtonItem

self.navigationItem.rightBarButtonItem

self.navigationItem.backBarButtonItem

self.navigationItem.titleView等等

注:1、这里的self 指的是UIViewController。

2、如果你在新视图中不修改backBarButtonItem 或leftBarButtonItem UINavigationController 会自动添加左边返回按钮用以返回了一个视图。总体的显示原则如下:

 1)、Left side of the navigationBar 左按钮

  a)如果当前的viewController设置了leftBarButtonItem,则显示当前VC所自带的leftBarButtonItem。

  b)如果当前的viewController没有设置leftBarButtonItem,且当前VC不是rootVC的时候,则显示前一层VC的backBarButtonItem。如果前一层的VC没有显示的指定backBarButtonItem的话,系统将会根据前一层VC的title属性自动生成一个back按钮,并显示出来。

  c)如果当前的viewController没有设置leftBarButtonItem,且当前VC已是rootVC的时候,左边将不显示任何东西。

  此处注意:5.0中新增加了一个属性leftItemsSupplementBackButton,通过指定该属性为YES,可以让leftBarButtonItem和backBarButtonItem同时显示,其中leftBarButtonItem显示在backBarButtonItem的右边。

  2)、title 标题

  a)如果当前VC通过 .navigationItem.titleView指定了自定义的titleView,系统将会显示指定的titleView,此处要注意自定义titleView的高度不要超过navigationBar的高度,否则会显示出界。

  b)如果当前VC没有指定titleView,系统则会根据当前VC的title或者当前VC的navigationItem.title的内容创建一个UILabel并显示,其中如果指定了navigationItem.title的话,则优先显示navigationItem.title的内容。

  3)、Right side of the navigationBar  右按钮

  a)如果当前VC指定了rightBarButtonItem的话,则显示指定的内容。

  b)如果当前VC没有指定rightBarButtonItem的话,则不显示任何东西。

 

参考:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

UINavigationBar 部分

切记,UINavigationBar只有一个,它永远只属于UINavigationController,是所有UIViewController公用的。所以,当你修改了UINavigationBar的背景图片或者颜色时,相当于修改了所有UIViewController的NavigationBar的背景图片或颜色。

下面,通过如下视图,我们来了解以下,当系统展示某个具体的UIViewController时,整个页面所有元素的从属关系。

参考:http://www.cnblogs.com/iOS-dd/archive/2013/06/12/3132366.html

uinavigationController、uinavigationBar、uinavigationBarItem三者的区别,现在明白了吗?

通俗地说就是,uinavigationController是个容器,里面可以装很多uiviewController。装这么多uiviewController让用户怎么控制它们呢,总得有个工具吧。这个工具就是uinavigationBar。一个容器就这么一个bar,相当于控制台吧。但是,管理那么多uiviewController,控制台上得按钮啊、标题啊,都千篇一律是不是看起来太无聊了。为了解决这个问题,uinavigationController为每个uiviewController生成一个uinavigationBarItem,通过这个uinavigationBarItem可以改变控制台“上面”得按钮和标题。如果你不自定义uinavigationBarItem,uinavigationController会使用默认的。

  • l    

UINavigationController是UIViewController的子类,UINavigationBar是UIView的子类。

UINavigationBar是UINavigationController的一个组成部分,就是上面的那个导航栏。

UINavigationBar又有UINavigationItem组成。

UINavigationItem则有title,按钮,提示文本等组成,就是我们看到的title文字,右上角的按钮。 

  • l   navigation item在navigation Bar代表一个viewController,具体一点儿来说就是每一个加到navigationController的viewController都会有一个对应的navigationItem
  • l   一个导航控制器控制多个视图,NavigationBar上的leftItem,rightItem,title是由当前的视图控制器控制的

 

所有参考:

UINavigationController使用详解  写的不错  http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

UINavigationController使用的一些技巧  讲的不错

UINavigationController,UINavigationBar  全面但 凌乱

UINavigationController    讲的比较直白

IOS开发笔记_3.UINavigationController层次关系   绝对牛b清晰

 

实践经验

1、self.navigationItem.leftBarButtonItem如何自定义位置:右移

方案:

复制代码
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ITEM_WIDTH, ITEM_HEIGHT)];[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];[btn setImage:[UIImage imageNamed:@"naviationbar_button_normal.png" ] forState:UIControlStateNormal];//不要用setbackgroudimage[btn setImage:[UIImage imageNamed:@"naviationbar_button_pressed.png" ] forState:UIControlStateHighlighted];btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);  //方案UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];self.navigationItem.leftBarButtonItem = backItem;
复制代码

来源:http://bbs.csdn.net/topics/390609070

2、隐藏 TabBar 的方法1(推荐)

如在A_VC中,push B_VC, 则在B_VC中写如下代码

复制代码
-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];        self.tabBarController.tabBar.hidden = YES;        //隐藏TabBar    [self.navigationItem setHidesBackButton:YES];     //隐藏NavigationController自动生成的返回按钮    self.navigationController.navigationBar.tintColor = [UIColor clearColor]; //返回按钮颜色}
复制代码

隐藏 TabBar 的方法2

在A_VC中中写如下代码

复制代码
-(IBAction)btnOnClicked:(id)sender{    A_VC *a_VC = [[[A_VC alloc]init]autorelease];    self.hidesBottomBarWhenPushed = YES;    //用于隐藏tabBar    [self.navigationController pushViewController:a_VC animated:YES];    self.hidesBottomBarWhenPushed = NO;}

0 0
原创粉丝点击