今天在做一個(gè)項(xiàng)目時(shí),又遇到一個(gè)奇葩的問(wèn)題:在右滑返回到一半時(shí),突然再往回拉,這時(shí)候?qū)Ш娇刂破髦蟮淖涌刂破鞯臉?biāo)題都錯(cuò)亂了,本來(lái)設(shè)置好的標(biāo)題一閃而過(guò),然后出現(xiàn)錯(cuò)誤的標(biāo)題。因?yàn)樵鹊捻?xiàng)目中沒(méi)有這個(gè)問(wèn)題,后來(lái)對(duì)比后才發(fā)現(xiàn),原來(lái)這個(gè)項(xiàng)目中的rootViewController的導(dǎo)航欄設(shè)置為了隱藏,其他子控制器導(dǎo)航欄不隱藏,問(wèn)題就出現(xiàn)在這里,我原先的設(shè)置方法是這樣的在viewWillAppear方法中設(shè)置self.navigationController.navigationBarHidden = YES;
是的,就是這句話導(dǎo)致了問(wèn)題的出現(xiàn),后來(lái)改為了加上動(dòng)畫(huà)隱藏就OK了,[self.navigationController setNavigationBarHidden:YES animated:YES];
iOS7中視圖控制器之間的導(dǎo)航自帶了手勢(shì)返回的功能,這個(gè)功能默認(rèn)就是存在的,但是當(dāng)我們自定義導(dǎo)航控制器的返回按鈕時(shí),常常會(huì)導(dǎo)致手勢(shì)返回功能失效。
iOS7自帶的手勢(shì)返回,主要用到的是UINavigationController類的interactivePopGestureRecognizer屬性,在上述原因?qū)е碌氖謩?shì)返回功能失效情況發(fā)生時(shí),可以通過(guò)重新設(shè)置interactivePopGestureRecognizer的代理,來(lái)使這個(gè)手勢(shì)重新可用,網(wǎng)上有很多解決方案,一般會(huì)把interactivePopGestureRecognizer.delegate設(shè)置為導(dǎo)航控制器自己即可。
這樣設(shè)置之后還會(huì)遇到許多問(wèn)題,需要各個(gè)擊破,比如push的過(guò)程中激活手勢(shì)會(huì)導(dǎo)致crash,在rootViewController中滑動(dòng)的時(shí)候,再想push到下一個(gè)頁(yè)面界面會(huì)卡死在那兒,解決方案通常是在UINavigationController的代理方法或重寫(xiě)UINavigationController的方法,在合適的時(shí)刻禁用手勢(shì)與重啟手勢(shì)來(lái)實(shí)現(xiàn)。如下面代碼:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
[super pushViewController:viewController animated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
if (navigationController.viewControllers.count == 1) {
navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
此外,還有可能會(huì)遇到在使用右滑手勢(shì)返回到一半時(shí),導(dǎo)航欄上出現(xiàn)三個(gè)藍(lán)點(diǎn)的情況,這個(gè)問(wèn)題通常設(shè)置子控制器的self.navigationItem.title = @""即可解決。