UINavigationBar

本文為大地瓜原創(chuàng),歡迎知識(shí)共享凰荚,轉(zhuǎn)載請(qǐng)注明出處燃观。
雖然你不注明出處我也沒(méi)什么精力和你計(jì)較。
作者微信號(hào):christgreenlaw


重溫UINavigationBar的點(diǎn)點(diǎn)滴滴便瑟。
本文是閱讀UINavigationBar文檔的學(xué)習(xí)記錄仪壮。

文中的藍(lán)色鏈接,可以調(diào)用Xcode直接打開胳徽。

UINavigationBar是支持navigation層級(jí)結(jié)構(gòu)的可視化控制組件积锅,基本上多用于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時(shí)养盗,控制navigation bar 的步驟:

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

在IB或者代碼中創(chuàng)建navigation controller缚陷。

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

使用UINavigationController對(duì)象的navigationBar屬性來(lái)配置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.

通過(guò)設(shè)置每個(gè)你push到navigation controller棧上的UIViewController的titlenavigationItem 屬性來(lái)控制navigation bar的內(nèi)容往核。

不使用navigation controller獨(dú)立使用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.

設(shè)置好Auto Layout約束來(lái)管理navigation bar在界面中的位置箫爷。

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

創(chuàng)建一個(gè)root navigation item來(lái)提供初始標(biāo)題

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

配置一個(gè)代理對(duì)象以處理用戶與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.

配置應(yīng)用以在相關(guān)的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 會(huì)自動(dòng)成為navigation bar 的代理對(duì)象。所以使用navigation controller時(shí)虎锚,不要自定義代理對(duì)象來(lái)處理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.

要訪問(wèn)和 navigation controller相關(guān)聯(lián)的navigation bar,應(yīng)該使用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.

想要實(shí)現(xiàn)自己的navigation跳轉(zhuǎn)邏輯效斑,使用navigation bar的UI,此時(shí)可以使用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的時(shí)候柱徙,你就需要為其提供內(nèi)容了缓屠。navigation bar不能直接添加子視圖。你需要使用UINavigationItem的實(shí)例护侮,來(lái)指明你要用什么按鈕敌完、什么自定義view。navigation item 有左右中三個(gè)內(nèi)容羊初。

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管理一個(gè)棧滨溉,棧中有很多個(gè)UINavigationItem對(duì)象。雖然這個(gè)棧主要是用來(lái)支持navigation controller的长赞,你也可以用它來(lái)實(shí)現(xiàn)你自己的自定義navigation 界面晦攒。棧中最頂部的item是當(dāng)前navigation bar所展示的內(nèi)容對(duì)應(yīng)的item。用pushNavigationItem:animated:方法向棧頂部推一個(gè)navigation item涧卵,用popNavigationItemAnimated:將item彈出。這兩個(gè)行為是可以帶動(dòng)畫的腹尖。

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柳恐,你也可以通過(guò)items屬性或者setItems:animated:方法來(lái)直接設(shè)置stack中的內(nèi)容。你可能需要用這個(gè)方法在啟動(dòng)時(shí)恢復(fù)到上次的界面狀態(tài)热幔,或者一次性push和pop多個(gè)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屬性賦值一個(gè)代理對(duì)象绎巨,用這個(gè)代理對(duì)象截獲navigation bar 的消息近尚。代理對(duì)象服從UINavigationBarDelegate協(xié)議,代理通知可以讓你知道棧中的push 和pop活動(dòng)场勤。用這個(gè)活動(dòng)通知來(lái)完成你的其他界面功能戈锻。

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.

兩種標(biāo)準(zhǔn)樣式:黑底白字,白底黑字和媳。barStyle可以選擇樣式格遭。對(duì)navigation bar外觀進(jìn)行的改動(dòng)將會(huì)覆蓋從barStyle推斷出的樣式。(就是說(shuō)你可以修改默認(rèn)的樣式)

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默認(rèn)是半透明的留瞳。(背景色是半透明的)你可以將 translucent設(shè)置為NO使navigation bar變?yōu)椴煌该鳌?/p>

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的背景色拒迅。修改這個(gè)屬性將會(huì)覆蓋barStyle推斷出的默認(rèn)顏色。對(duì)于所有的UIView子類來(lái)說(shuō),只需要使用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前硫。對(duì)應(yīng)的使用 NSFontAttributeName, NSForegroundColorAttributeName, and NSShadowAttributeName三個(gè)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:方法可以調(diào)整title的豎直位置。該方法允許你根據(jù)bar的高度(由 UIBarMetrics枚舉量表示)來(lái)調(diào)整位置嗤详。下圖展示了一個(gè)自定義的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:方法,在適當(dāng)?shù)奈恢锰峁┮粋€(gè)UIImage對(duì)象苍狰。在bar position參數(shù)用UIBarPosition值以確定將提供的圖片放在底部還是頂部办龄,如果它出現(xiàn)在頂部,要不要將其延伸到status bar底部淋昭。相似地俐填,你可以指明圖片應(yīng)該是compact還是默認(rèn)的bar那么大,通過(guò)提供一個(gè)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 屬性賦值一個(gè)resizable的UIImage以添加陰影英融。要使用自定義的陰影圖片,你需要先指明一個(gè)自定義的背景圖片歇式。下圖展示了有自定義背景圖片的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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末痕鳍,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子龙巨,更是在濱河造成了極大的恐慌笼呆,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旨别,死亡現(xiàn)場(chǎng)離奇詭異抄邀,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)昼榛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門境肾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)剔难,“玉大人,你說(shuō)我怎么就攤上這事奥喻∨脊” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵环鲤,是天一觀的道長(zhǎng)纯趋。 經(jīng)常有香客問(wèn)我,道長(zhǎng)冷离,這世上最難降的妖魔是什么吵冒? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮西剥,結(jié)果婚禮上痹栖,老公的妹妹穿的比我還像新娘。我一直安慰自己瞭空,他們只是感情好揪阿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著咆畏,像睡著了一般南捂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上旧找,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天溺健,我揣著相機(jī)與錄音,去河邊找鬼钮蛛。 笑死鞭缭,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的愿卒。 我是一名探鬼主播缚去,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼潮秘,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼琼开!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起枕荞,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤柜候,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后躏精,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體渣刷,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年矗烛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了辅柴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖碌嘀,靈堂內(nèi)的尸體忽然破棺而出涣旨,到底是詐尸還是另有隱情,我是刑警寧澤股冗,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布霹陡,位于F島的核電站,受9級(jí)特大地震影響止状,放射性物質(zhì)發(fā)生泄漏烹棉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一怯疤、第九天 我趴在偏房一處隱蔽的房頂上張望浆洗。 院中可真熱鬧,春花似錦旅薄、人聲如沸辅髓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)洛口。三九已至,卻和暖如春凯沪,著一層夾襖步出監(jiān)牢的瞬間第焰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工妨马, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挺举,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓烘跺,卻偏偏與公主長(zhǎng)得像湘纵,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子滤淳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容