在iOS7的時候,水果推出了一個功能,就是當你在當前頁面時,如果該頁面是由上一個頁面push出來的,那么在屏幕左側邊緣側滑可以快速回到上一個頁面,這個功能對于大屏手機來說是十分方便的。但是,在我們實現某個頁面UI布局后,可能會發現這個功能失效了,在這里來記錄一下解決方法。
首先要說的是NavigationController的左按鈕的展現順序:
- 第二頁的leftBarButtonItem
- 第一頁的backBarButtonItem
- 第一頁的title
- 默認顯示的 <back
接下來要說側滑失效的原因:
如果我們使用的是系統級別的UINavigationController,并且我們第二個頁面的左按鈕是通過展示第二個頁面的leftBarButtonItem來實現的,那么此時這個功能就會失效。
解決方法
自定義一個UINavigationController,繼承自UINavigationController,通過設置他推出頁面時的代理來解決側滑返回失效問題。
.h文件
#import <UIKit/UIKit.h>
@interface MyNavigationController : UINavigationController
@end
.m文件
#import "myNavigationController.h"
@interface MyNavigationController ()<UINavigationControllerDelegate>
@property (nonatomic, weak) id PopDelegate;
@end
@implementation MyNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.PopDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self.viewControllers[0]) {
self.interactivePopGestureRecognizer.delegate = self.PopDelegate;
}else{
self.interactivePopGestureRecognizer.delegate = nil;
}
}
@end