UINavigationBar

本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号:christgreenlaw


重温UINavigationBar的点点滴滴。
本文是阅读UINavigationBar文档的学习记录。

文中的蓝色链接,可以调用Xcode直接打开。

UINavigationBar是支持navigation层级结构的可视化控制组件,基本上多用于navigation controller。


Overview

主要的组件:
a left(back) button, a center title, and an optional right button.


Navigation bar

You can use a navigation bar as a standalone object or in conjunction with a navigation controller object.

The NavigationController object creates, displays, and manages its associated navigation bar, and uses attributes of the view controllers you add to control the content displayed in the navigation bar.


使用navigation controller时,控制navigation bar 的步骤:

  1. Create a navigation controller in Interface Builder or in the code.

在IB或者代码中创建navigation controller。

  1. Configure the appearance of the navigation bar using the navigationBar property on the UINavigationController object.

使用UINavigationController对象的navigationBar属性来配置navigation bar的外观

  1. Control the content of the navigation bar by setting the title and navigationItem properties on each UIViewController you push onto the navigation controller’s stack.

通过设置每个你push到navigation controller栈上的UIViewController的titlenavigationItem 属性来控制navigation bar的内容

不使用navigation controller独立使用navigation bar:
You can also use a standalone navigation bar, without using a navigation controller. To add a navigation bar to your interface, the following steps are required:

  1. Set up Auto Layout rules to govern the position of the navigation bar in your interface.

设置好Auto Layout约束来管理navigation bar在界面中的位置。

  1. Create a root navigation item to supply the initial title.

创建一个root navigation item来提供初始标题

  1. Configure a delegate object to handle user interactions with the navigation bar.

配置一个代理对象以处理用户与navigation bar之间的交互

  1. Customize the appearance of the navigation bar.

配置bar的外观

  1. Configure your app to push and pop relevant navigation items as the user navigates through the hierarchical screens.

配置应用以在相关的navigation item之间push和pop。

Using a Navigation Bar with a Navigation Controller

If you use a navigation controller to manage the navigation between different screens of content, the navigation controller creates a navigation bar automatically and pushes and pops navigation items when appropriate.
A navigation controller uses the navigationItem property on UIViewController to provide the model objects to its navigation bar when navigating a stack of view controllers. The default navigation item uses the view controller’s title, but you can override the navigationItem on a UIViewController subclass to gain complete control of the navigation bar’s content.
A navigation controller automatically assigns itself as the delegate of its navigation bar object. Therefore, when using a navigation controller, don’t assign a custom delegate object to the corresponding navigation bar.

navigation controller 会自动成为navigation bar 的代理对象。所以使用navigation controller时,不要自定义代理对象来处理navigation bar的行为。

To access the navigation bar associated with a navigation controller, use the navigationBar property on UINavigationController. See Customizing the Appearance of a Navigation Bar for details on how to customize the appearance of a navigation bar.

要访问和 navigation controller相关联的navigation bar,应该使用UINavigationControllernavigationBar属性。

For information about navigation controllers, see UINavigationController.

Adding Content to a Standalone Navigation Bar

In the vast majority of scenarios you will use a navigation bar as part of a navigation controller. However, there are situations for which you might want to use the navigation bar UI and implement your own approach to content navigation. In these situations, you can use a standalone navigation bar.

想要实现自己的navigation跳转逻辑,使用navigation bar的UI,此时可以使用standalone navigation bar.

When you use a navigation bar as a standalone object, you are responsible for providing its content. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string. Figure 1 shows how the navigation item properties manifest themselves in a navigation bar.

使用standalone navigation bar的时候,你就需要为其提供内容了。navigation bar不能直接添加子视图。你需要使用UINavigationItem的实例,来指明你要用什么按钮、什么自定义view。navigation item 有左右中三个内容。

A navigation bar manages a stack of UINavigationItem objects. Although the stack is there mostly to support navigation controllers, you can use it to implement your own custom navigation interface. The topmost item in the stack represents the navigation item whose contents are currently displayed by the navigation bar. You push new navigation items onto the stack using the pushNavigationItem:animated: method and pop items off the stack using the popNavigationItemAnimated: method. Both of these changes can be animated for the benefit of the user.

navigation bar管理一个栈,栈中有很多个UINavigationItem对象。虽然这个栈主要是用来支持navigation controller的,你也可以用它来实现你自己的自定义navigation 界面。栈中最顶部的item是当前navigation bar所展示的内容对应的item。用pushNavigationItem:animated:方法向栈顶部推一个navigation item,用popNavigationItemAnimated:将item弹出。这两个行为是可以带动画的。

In addition to pushing and popping items, you can also set the contents of the stack directly using either the items property or the setItems:animated: method. You might use this method at launch time to restore your interface to its previous state or to push or pop more than one navigation item at a time.Figure 2 shows the part of the UINavigationBar API responsible for managing the stack of navigation items.

除了push和pop item,你也可以通过items属性或者setItems:animated:方法来直接设置stack中的内容。你可能需要用这个方法在启动时恢复到上次的界面状态,或者一次性push和pop多个navigation item。下图展示了控制navigation item栈的 UINavigationBar API。

Navigation bar stack management

If you are using a navigation bar as a standalone object, assign a custom delegate object to the delegate property and use that object to intercept messages coming from the navigation bar. Delegate objects must conform to the UINavigationBarDelegateprotocol. The delegate notifications let you track when navigation items are pushed or popped from the stack. You use these notifications to update the rest of your app’s user interface.

使用standalone的navigation bar就需要为其delegate属性赋值一个代理对象,用这个代理对象截获navigation bar 的消息。代理对象服从UINavigationBarDelegate协议,代理通知可以让你知道栈中的push 和pop活动。用这个活动通知来完成你的其他界面功能。

For more information about creating navigation items, see UINavigationItem. For more information about implementing a delegate object, see UINavigationBarDelegate.

Customizing the Appearance of a Navigation Bar

Navigation bars have two standard appearance styles: white with dark text or black with light text. Use the barStyle property to select the style. Any changes you make to other navigation bar appearance properties override those inferred from the bar style.

两种标准样式:黑底白字,白底黑字。barStyle可以选择样式。对navigation bar外观进行的改动将会覆盖从barStyle推断出的样式。(就是说你可以修改默认的样式)

Navigation bars are translucent by default; that is, their background color is semitransparent. You can make the navigation bar opaque by setting the translucent property to NO.

navigation bar默认是半透明的。(背景色是半透明的)你可以将 translucent设置为NO使navigation bar变为不透明。

You can specify a custom tint color for a navigation bar background using the barTintColor property. Setting this property overrides the default color inferred from the bar style. As with all UIView subclasses, you can control the color of the interactive elements within navigation bars, including button images and titles, using the tintColorproperty.

barTintColor属性可以自定义navigation bar的背景色。修改这个属性将会覆盖barStyle推断出的默认颜色。对于所有的UIView子类来说,只需要使用tintColor属性,你就可以控制navigation bar上所有可交互元素的颜色,包括按钮图片和title。

The titleTextAttributes property specifies the attributes for displaying the bar’s title text. You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary using the NSFontAttributeName, NSForegroundColorAttributeName, and NSShadowAttributeName keys, respectively. For more information about string-formatting attributes, see Character Attributes.

titleTextAttributes属性表明了bar上title text的attributes。对应的使用 NSFontAttributeName, NSForegroundColorAttributeName, and NSShadowAttributeName三个key可以在text attributes dictionary中指明字体,字色,字阴影,字阴影偏移量。

Use the setTitleVerticalPositionAdjustment:forBarMetrics: method to adjust the vertical position of the title. This method allows you to specify the adjustment dependent on the bar height, which is represented by the UIBarMetrics enum.Figure 3 shows a navigation bar with custom tint color, title text attributes and bar tint color.

setTitleVerticalPositionAdjustment:forBarMetrics:方法可以调整title的竖直位置。该方法允许你根据bar的高度(由 UIBarMetrics枚举量表示)来调整位置。下图展示了一个自定义的bar。

Navigation bar fonts and colors

To allow complete customization over the appearance of navigation bars, you can additionally provide custom background and shadow images. To provide a custom background image, use the setBackgroundImage:forBarPosition:barMetrics: method, providing a UIImage object for the appropriate bar position and metrics values. Use a UIBarPosition value for the bar position argument to specify whether to use the supplied image at the bottom or the top of the window, and if it appears at the top, whether to extend it upward under the status bar. Similarly, you can specify that the image should be used for either compact or default bar metrics, with or without a prompt, by providing a UIBarMetrics value to the bar metrics argument.

要完全自定义navigation bar的外观,可以额外提供自定义的背景和阴影图。要提供自定义的背景图片,使用setBackgroundImage:forBarPosition:barMetrics:方法,在适当的位置提供一个UIImage对象。在bar position参数用UIBarPosition值以确定将提供的图片放在底部还是顶部,如果它出现在顶部,要不要将其延伸到status bar底部。相似地,你可以指明图片应该是compact还是默认的bar那么大,通过提供一个UIBarMetrics值。

To add a shadow, provide a resizable UIImage to the shadowImage property. To use the custom shadow image, you need to have specified a custom background image.Figure 4shows a navigation bar with a custom background image, supplied using setBackgroundImage:forBarPosition:barMetrics: with a bar position value of UIBarPositionTopAttached and a bar metrics value of UIBarMetricsDefault. A custom image has also been provided to the shadowImage property.

shadowImage 属性赋值一个resizable的UIImage以添加阴影。要使用自定义的阴影图片,你需要先指明一个自定义的背景图片。下图展示了有自定义背景图片的navigation bar,supplied using setBackgroundImage:forBarPosition:barMetrics: with a bar position value of UIBarPositionTopAttached and a bar metrics value of UIBarMetricsDefault.自定义的阴影图片由shadowImage 属性提供。

Navigation bar with custom background and shadow images

Interface Builder Attributes

Table 1 lists the core attributes that you configure for navigations bars in the Attributes Inspector within Interface Builder.

可在IB的Attributes Inspector中配置的主要属性。

Attribute Description
Style Specifies the UI bar style to apply to the navigation bar. The bar style controls the title color and the bar tint color, but you can override it by providing values for those attributes. Select Translucent to make the navigation bar semitransparent. Access these values at runtime with the barStyle and translucent properties.
Bar Tint Controls the tint color of the navigation bar. This overrides the value implied by the Style attribute. If the Translucent attribute is selected, the Bar Tint color is automatically made semitransparent. Access this value at runtime with the barTintColor property.
Shadow Image Represents the image used as a shadow beneath the navigation bar. This image is stretched horizontally to match the width of the bar. Access this value at runtime with the shadowImage property.
Back Image Specifies the image that appears at the leading edge of the back button. This attribute must be used in combination with the Back Mask attribute. Access this value at runtime with the backIndicatorImage property.
Back Mask Specifies the mask associated with the Back Image attribute. This is used to control the appearance of the Back button during animated transitions, and therefore must be used in conjunction with the Back Image attribute. Access this value at runtime with the backIndicatorTransitionMaskImage property.

Table 2 lists the Interface Builder attributes that affect the appearance of the navigation bar’s title.

Attribute Description
Title Font The font used to render the title in the center of the navigation bar. Access this value at runtime with the value stored against the NSFontAttributeName key in the dictionary in the titleTextAttributes property.
Title Color The color used to render the navigation bar title. Access this value at runtime using the NSForegroundColorAttributeName key in the dictionary in the titleTextAttributes property.
Title Shadow Specifies the color and offset of the shadow used when rendering the navigation bar’s title. Access these values at runtime with the dictionary in the [titleTextAttributes] property, using the NSShadowAttributeName key.

Internationalization

To internationalize navigation bars, specify a localized string for each of the displayed string properties of the navigation item model objects.
For more information about internationalizing your interface, see Internationalization and Localization Guide.

Accessibility

Navigation bars are accessible by default. The default accessibility trait for a navigation bar is User Interaction Enabled.
With VoiceOver enabled on an iOS device, after the user navigates to a new view in the hierarchy, VoiceOver reads the navigation bar’s title, followed by the name of the left bar button item. When the user taps an element in a navigation bar, VoiceOver reads the name and the type of the element; for example, "General back button," "Keyboard heading," and "Edit button."
For general information about making your interface accessible, see Accessibility Programming Guide for iOS.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容