iOS 導(dǎo)航欄上那些事

一、側(cè)滑返回

側(cè)滑返回手勢是從iOS7開始增加的一個返回操作,經(jīng)歷了兩年時間估計iPhone用戶大部分都已經(jīng)忽略了屏幕左上角那個礙眼的back按鈕了。之前在網(wǎng)上搜過有關(guān)側(cè)滑手勢的技術(shù)博客,發(fā)現(xiàn)大多比較散亂,甚至有很多都是簡單的粘貼復(fù)制,并不全面。側(cè)滑返回的操作效果與左上角的back按鈕是一樣的,所以一起放在這里進行探討。

導(dǎo)航欄左上角的back按鈕是附著在UINavigationController的UINavigationBar里自帶的一個返回按鈕,導(dǎo)航欄自帶的back按鈕的圖層結(jié)構(gòu)如下圖所示。一個UINavigationController只會有一個UIBackButtonContentView,但是可以有多個leftBarButtonItem、rightBarButtonItem(leftBarButtonItem、rightBarButtonItem就在下圖所示的UIButtonBarStackView圖層下),其中backButton與leftBarButtonItem之間的關(guān)系和區(qū)別在后面我們會講到。

回到頂部

一 側(cè)滑返回

側(cè)滑返回是系統(tǒng)iOS7自帶的一種方便用戶進行返回操作而推出的一種新功能。在開發(fā)過程中,對側(cè)滑返回進行控制非常簡單,主要就是啟動側(cè)滑手勢和禁用側(cè)滑手勢。首先,我們來看一下UINavigationController的@property,可以找到下面這個屬性。

1.1 側(cè)滑開啟與關(guān)閉

  UINavigationController的interactivePopGestureRecognizer這個屬性就是我們的側(cè)滑返回手勢,如果你的項目中沒有需求要自定義返回按鈕(雖然我覺得這并不太可能),那么你所需要的操作就非常簡單了,不多說直接上代碼。

1.2 側(cè)滑使用注意

側(cè)滑手勢在使用中需要注意的一點就是在項目開發(fā)中,我們一般是采用的UITabBar +?UINavigationController架構(gòu),對于每一個UITabBar的item模塊,我們都定義一個UINavigationController對該item模塊上的viewController進行控制。而在這個模塊上,我們有某個或某些viewController需要禁用側(cè)滑手勢(一般需要禁用側(cè)滑手勢是因為返回或退出當前viewController時需要double confirm,在一些填表的頁面比較常見),而其他的viewController則不需要禁用側(cè)滑手勢。這時候我們就需要特別小心,因為self.navigationController.interactivePopGestureRecognizer.enabled = NO;?//禁用側(cè)滑手勢是對當前的UINavigationController有效的,所以一旦你在某個界面禁用了側(cè)滑,那么該UINavigationController控制下的所有viewController都會禁用側(cè)滑,這顯然是不合理的。提供一個解決方案就是在進入viewController時- (void)viewDidAppear:(BOOL)animated;中禁用側(cè)滑手勢,然后在離開viewController時- (void)viewWillDisappear:(BOOL)animated;開啟策劃手勢。具體代碼如下:

- (void)viewDidAppear:(BOOL)animated

{

? ?[super viewDidAppear:animated];

? ? // 禁用返回手勢if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

? ? self.navigationController.interactivePopGestureRecognizer.enabled = NO;

? ? }

}

- (void)viewWillDisappear:(BOOL)animated

{

? ? [super viewWillDisappear:animated];

? ? // 開啟返回手勢if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

? ? self.navigationController.interactivePopGestureRecognizer.enabled = YES;

? ? }

}


1.3 側(cè)滑手勢的獲取

如果一個頁面上有多個手勢,我們要如何去獲取策劃手勢,并對其進行操作呢?其實很簡單,側(cè)滑手勢是一種UIScreenEdgePanGestureRecognizer,所以我們只需要對當前手勢的類別進行判斷就可以了。具體代碼如下:

//獲取側(cè)滑返回手勢

- (UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer

{

? ? UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = nil;

? ? if (self.view.gestureRecognizers.count > 0)

? ? {

? ? ? ? for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)

? ? ? ? {

? ? ? ? ? ? if ([recognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? screenEdgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)recognizer;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return screenEdgePanGestureRecognizer;

}

1.4 UIScrollView與側(cè)滑手勢共存問題

UIScrollView及其子類自帶滑動手勢,所以如果一個viewController鐘有UIScrollView及其子類的view時,側(cè)滑手勢影響用戶體驗效果,此時用戶將無法通過側(cè)滑進行返回。因為側(cè)滑返回手勢事實上是由存在已久的UIPanGestureRecognizer來識別并響應(yīng)的,它直接與UINavigationController的view進行綁定,因此在包含UIScrollView的viewController中存在如下關(guān)系:

UIPanGestureRecognizer      ? ??——bind—— ?UIScrollView

UIScreenEdgePanGestureRecognizer ——bind—— ?UINavigationController.view

  滑動返回?zé)o法觸發(fā),說明UIScreenEdgePanGestureRecognizer并沒有接收到手勢事件,也就是說UIScreenEdgePanGestureRecognizer被UIPanGestureRecognizer屏蔽了。因此,我們?yōu)榱藢崿F(xiàn)側(cè)滑返回手勢,我們需要設(shè)置兩種手勢的共存和先后響應(yīng)問題,我們可以設(shè)置UIScrollView的UIPanGestureRecognizer手勢在UIScreenEdgePanGestureRecognizer失效時才識別,具體設(shè)置方法如下:

//指定滑動手勢在側(cè)滑返回手勢失效后響應(yīng)

[self.tableView.panGestureRecognizer requireGestureRecognizerToFail:[self.navigationController?screenEdgePanGestureRecognizer]];

回到頂部

?二 導(dǎo)航欄的back按鈕

在了解導(dǎo)航欄的返回按鈕之前,我們先了解一下導(dǎo)航欄管理導(dǎo)航欄上各類控件的UINavigationBar。首先,我們先來看一看官方文檔怎么介紹UINavigationBar,AUINavigationBarobject is a bar, typically displayed?at the top of the window, containing buttons for navigating within a hierarchy of screens.The primary components are a left (back) button, a center title, and an optional right button.You can use a navigation bar as a standalone object or in conjunction with a navigation controller object.組成如下圖左邊所示。最重要的一部分我用藍色加粗標出來了,就是說這個UINavigationBar主要是由左右按鈕控件、中間標題控件組成。原生的導(dǎo)航條上的返回(back)按鈕,一般是顯示一個返回箭頭+上一頁面的標題(或者是 返回箭頭+Back),如下圖右邊所示。

2.1 導(dǎo)航條上的按鈕三兄弟

在前面我們也提到了,在導(dǎo)航欄上有左右按鈕和返回按鈕,官方稱謂是backBarButtonItem、leftBarButtonItem、rightBarButtonItem。他們都屬于UINavigationItem的組成部分,都顯示在navigationBar上,都屬于UIBarButtonItem類,所以我給他們?nèi)∶麨閷?dǎo)航條上的按鈕三兄弟。

  首先,我們來說一下leftBarButtonItem、rightBarButtonItem,這兩個是孿生兄弟,唯一的區(qū)別就是在導(dǎo)航條上的位置,顧名思義,leftBarButtonItem在導(dǎo)航條左側(cè),rightBarButtonItem在導(dǎo)航條右側(cè)。此外,還有一點需要我們注意的是navigationBar上的leftBarButtonItem、rightBarButtonItem可以有多個,用法也非常簡單,常見用法就是一般在- (void)viewDidLoad?中添加按鈕,然后添加按鈕的點擊功能即可。

//添加取消btn

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(navBtnPress:)] ;

cancelBtn.tag = 1000 ;

self.navigationItem.leftBarButtonItem = cancelBtn ;

//添加完成btn

UIBarButtonItem *createBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(navBtnPress:)] ;

createBtn.enabled = NO ;? ? ? ? //剛開始設(shè)置為不可選

createBtn.tag = 1001 ;

self.navigationItem.rightBarButtonItem = createBtn ;

/**

導(dǎo)航欄 取消 完成 按鈕的操作

@param sender <#sender description#>

*/

- (void) navBtnPress:(UIButton *)sender{

? ? if (sender.tag == 1000) {

? ? ? ? //取消

? ? ? ? NSLog(@"cancel");

? ? } else {

? ? ? ? //完成

? ? ? ? NSLog(@"create");

? ? }

}

2.2 導(dǎo)航條上的backBarButtonItem

同樣的,我們首先從官方文檔了解一下backBarButtonItem的描述:When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item.?When this property isnil, the navigation item uses the value in itstitleproperty to create an appropriate back button. If you want to specify a?custom image or title?for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item?ignores?custom views in the back bar button anyway.說明了backBarButtonItem只能自定義image和title,不能重寫target or action,系統(tǒng)會忽略其他的相關(guān)設(shè)置項。

Note:?If the title of your back button is too long to fit in the available space on the navigation bar, the navigation bar may substitute the string “Back” in place of the button’s original 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 theleftBarButtonItemsorleftBarButtonItemproperty of its navigation item—the navigation bar does not change the button title.這段描述了關(guān)于backBarButtonItem的一些特殊點,如果你上一級設(shè)置的backBarButtonItem的標題過長(沒有設(shè)置則默認是上一級標題),那么系統(tǒng)可能會自動用“Back/返回”來代替返回按鈕中的標題。此外,如果是自定義的左按鈕,則系統(tǒng)不會修改其值。

2.3?backBarButtonItem和leftBarButtonItem的區(qū)別

backBarButtonItem和另外兩兄弟是有區(qū)別的,比如當前有AController準備push到BController,設(shè)置backBarButtonItem的title和image需要在AController內(nèi)設(shè)置,在調(diào)用AController Push:B之前進行設(shè)置,AController.navigationItem.backBarButtonItem = ....,其他兩兄弟則是在BController的ViewDidload后設(shè)置均可。所以,如果我們一定需要重寫返回鍵的action做一些其他的工作,則需要自定義一leftBarButtonItem,因為系統(tǒng)定義leftBarButtonItem的顯示優(yōu)先級比backBarButtonItem優(yōu)先級高,當存在leftBarButtonItem時,自動忽略backBarButtonItem,達到重寫backBarButtonItem的目的。

backBarButtonItem的自定義不會影響系統(tǒng)的側(cè)滑返回手勢,而leftBarButtonItem的自定義則會禁用側(cè)滑返回手勢。

backBarButtonItem的自定義不能影響返回按鈕的標題和圖片,不會隱藏最左邊的返回箭頭backIndicatorImage,而leftBarButtonItem的自定義則會使最左邊的返回箭頭消失backIndicatorImage。

2.4?各個對象下的backBarButtonItem的區(qū)別

這一部分參考自:http://blog.csdn.net/dreamno23/article/details/21085783,還有些沒弄明白,后面有時間再理一理這一塊。

對于導(dǎo)航欄上的按鈕三兄弟,我們在3個類下面都能發(fā)現(xiàn)他們,比如當前在一個UIViewController內(nèi),輸入以下方法都能發(fā)現(xiàn)他們。(同leftBarButtonItem |?rightBarButtonItem)

比如在AController->BController,在A設(shè)置了self.navigationItem.backBarButtonItem,經(jīng)過試驗發(fā)現(xiàn),這個backBarButtonItem為BController的self.navigationController.navigationBar.backItem.backBarButtonItem。

UIViewController的屬性navigationItem正是被當前UINavigationBar--[UINavigationBar?appearance]管理的屬性

self.navigationController.navigationItem.backBarButtonItem則是表示當前navigationController的parent的UINavigationBar,一般情況下沒有這樣的嵌套。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容