我們?cè)陂_(kāi)發(fā)過(guò)程中都會(huì)或多或少的遇到過(guò)自定義"返回"按鈕后系統(tǒng)返回手勢(shì)無(wú)法響應(yīng)的情況,下面做一總結(jié):
在iOS7系統(tǒng)中胜榔,自帶了可以通過(guò)右滑返回上一級(jí)頁(yè)面的手勢(shì)眯勾,如果僅僅修改leftBarButtonItem是無(wú)法響應(yīng)這個(gè)手勢(shì)的⊥拭停可以在pushViewController之后加入如下代碼:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//在自定義leftBarButtonItem后添加下面代碼就可以完美解決返回手勢(shì)無(wú)效的情況
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
可以通過(guò)下面方式進(jìn)行返回的事件的捕捉:
- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear: animated];
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
}
至此网杆,我們就完美解決了iOS7上使用leftBarButtonItem的滑動(dòng)返回問(wèn)題。
同時(shí)可以通過(guò)下面方式,來(lái)解決多次滑動(dòng)之后會(huì)導(dǎo)致界面假死的情況:
在所有除一級(jí)頁(yè)面之外的頁(yè)面的viewDidAppear和viewWillDisappear中加入以下代碼:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//代理置空伊滋,否則會(huì)閃退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//開(kāi)啟iOS7的滑動(dòng)返回效果
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//只有在二級(jí)頁(yè)面生效
if ([self.navigationController.viewControllers count] == 2) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
}
同時(shí)在UINavigationcontroller的delegate中實(shí)現(xiàn)以下方法:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//開(kāi)啟滑動(dòng)手勢(shì)
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
在pushviewcontroller之前加入以下代碼:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = NO;
}
[self.navigationController pushViewController:viewController animated:YES];
即可在實(shí)現(xiàn)滑動(dòng)返回的同時(shí)碳却,避免界面卡死的問(wèn)題。
同時(shí)我們?cè)趹?yīng)用中還會(huì)遇到一種情況:
在一個(gè)界面中會(huì)使用UIScrollView. 在這時(shí),會(huì)阻擋系統(tǒng)的向右返回手勢(shì),可以通過(guò)下面方式解決:
- (void)viewDidLoad {
[super viewDidLoad];
.....
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
實(shí)現(xiàn)代理方法:
- (BOOL)gestureRecognizer: (UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer: (UIGestureRecognizer *)otherGestureRecognizer
{
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return NO;
}
當(dāng)然不要忘記制空代理:<否則會(huì)崩潰>
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//代理置空笑旺,否則會(huì)閃退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
參考:
1: http://blog.csdn.net/zhaoxy_thu/article/details/15811189
2: http://ju.outofmemory.cn/entry/103566
轉(zhuǎn)載請(qǐng)標(biāo)注出處. 尊重原創(chuàng).