iOS7之后蘋果導(dǎo)航控制器增加了一個左側(cè)邊緣右滑pop的手勢,在非棧底的控制器里面,從左邊右滑之后相當于出棧(pop)樣子。
站在用戶的角度思考問題,與客戶深入溝通,找到佛坪網(wǎng)站設(shè)計與佛坪網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋佛坪地區(qū)。
以下設(shè)置均在自定義的UINavigationController里操作
// 默認是YES 設(shè)置為NO左側(cè)右滑手勢就不可用了 self.interactivePopGestureRecognizer.enabled = YES;
也可以通過手勢實現(xiàn)右滑
遵守UIGestureRecognizerDelegate協(xié)議,設(shè)置代理對象,實現(xiàn)一下代理方法即可:
-(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.navigationController.interactivePopGestureRecognizer.delegate = self; } #pragma mark - UIGestureRecognizerDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if (self.viewControllers.count <= 1) { return NO; } return YES; }
個人的記憶方法:
backBarButtonItem設(shè)置下一個頁面的返回按鈕
leftBarButtonItem設(shè)置當前頁面的返回按鈕
項目中我們多數(shù)會自定義左側(cè)返回按鈕leftBarButtonItem,這個時候就算設(shè)置self.interactivePopGestureRecognizer.enabled = YES了或者實現(xiàn)手勢的代理,左邊緣的右滑動手勢依舊會失效,例如如下代碼寫過之后,可以驗證一下,失效了。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewControllers.count > 0) { /* 不是棧底控制器的時候隱藏tabbar */ viewController.hidesBottomBarWhenPushed = YES; // 設(shè)置左邊的返回按鈕leftBarButtonItem UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(0, 0, 44, 44); backBtn.imageView.contentMode = UIViewContentModeCenter; backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 34); [backBtn setImage:[UIImage imageNamed:@"rrrr"] forState:UIControlStateNormal]; [backBtn setImage:[UIImage imageNamed:@"rrrr"] forState:UIControlStateHighlighted]; [backBtn addTarget: self action: @selector(back) forControlEvents: UIControlEventTouchUpInside]; UIBarButtonItem *backBtnItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn]; viewController.navigationItem.leftBarButtonItem = backBtnItem; // 此處改為設(shè)置backBarButtonItem 右滑手勢可以用,但自定義按鈕不展示?。? } [super pushViewController:viewController animated:animated]; }
按照如下方式設(shè)置backBarButtonItem或leftBarButtonItem,其他不做任何設(shè)置,右滑效果可用:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewControllers.count > 0) { /* 不是棧底控制器的時候隱藏tabbar */ viewController.hidesBottomBarWhenPushed = YES; } [self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"rrrr"]]; [self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"rrrr"]]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backItem; [super pushViewController:viewController animated:animated]; }
或者
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewControllers.count > 0) { /* 不是棧底控制器的時候隱藏tabbar */ viewController.hidesBottomBarWhenPushed = YES; [self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"rrrr"]]; [self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"rrrr"]]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.leftBarButtonItem = backItem; } [super pushViewController:viewController animated:animated]; }
進過上面的反復(fù)測試:在自定義導(dǎo)航欄控制器中backBarButtonItem不能用View視圖方法自定義,而leftBarButtonItem可以。
setBackIndicatorImage和setBackIndicatorTransitionMaskImage無論怎么設(shè)置都是只能看到setBackIndicatorImage的圖片,找不到原因?。?!
然后嘗試直接在root視圖控制器里面設(shè)置返回按鈕backBarButtonItem的自定義view,右滑手勢可用,但是設(shè)置leftBarButtonItem就不能使用右滑手勢:
- (void)viewDidLoad { [super viewDidLoad]; // 設(shè)置左邊的返回按鈕 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(0, 0, 44, 44); backBtn.imageView.contentMode = UIViewContentModeCenter; backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 34); [backBtn setImage:[UIImage imageNamed:@"rrrr"] forState:UIControlStateNormal]; [backBtn setImage:[UIImage imageNamed:@"rrrr"] forState:UIControlStateHighlighted]; [backBtn addTarget: self action: @selector(back) forControlEvents: UIControlEventTouchUpInside]; backBtn.backgroundColor = [UIColor redColor]; [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"rrrr"]]; [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"rrrr"]]; UIBarButtonItem *backBtnItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn]; backBtnItem.title = @""; self.navigationItem.backBarButtonItem = backBtnItem; }
以上是自己的驗證,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。