iOS app 右滑返回,iosapp滑返回
前提條件,存在A和B兩個頁面,A是主介面,A push 到 B
方法一:如果B頁面的返回按鈕要用自訂的按鈕(在iOS7中,如果使用了UINavigationController,那麼系統內建的附加了一個從螢幕左邊緣開始滑動可以實現pop的手勢。但是,如果自訂了navigationItem的leftBarButtonItem,那麼這個手勢就會失效。)
在A介面:
- (void)viewDidAppear:(BOOL)animated {
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;//重新設定代理
self.navigationController.interactivePopGestureRecognizer.enabled = NO;//本頁面不能右滑
}
在B裡面:
設定返回按鈕:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(5, 2, 21, 35);
[backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[[backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
[self.navigationController popViewControllerAnimated:YES];
}];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backItem;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;//最重要的一步
方法二:在A頁面裡面(A頁面裡面的backBarButtonItem其實就是我們看到的B頁面的返回按鈕,B頁面的leftBar是左側的按鈕,注意優先順序,B裡面有自訂leftBar,顯示B頁面的,如果沒有,看A介面,如果裡面有backBarButtonItem,顯示,如果沒有,就是系統預設的那個)
UIImage* image = [UIImage imageNamed:@"back"];
UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
[item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[item setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = item;
self.navigationItem.backBarButtonItem = item;
我採用的是方法一,方法二我沒有辦法調整返回按鈕的位置