前提條件拍棕,存在A和B兩個(gè)頁(yè)面掏呼,A是主界面蟆融,A push 到 B
方法一:如果B頁(yè)面的返回按鈕要用自定義的按鈕(在iOS7中,如果使用了UINavigationController属铁,那么系統(tǒng)自帶的附加了一個(gè)從屏幕左邊緣開(kāi)始滑動(dòng)可以實(shí)現(xiàn)pop的手勢(shì)眠寿。但是,如果自定義了navigationItem的leftBarButtonItem焦蘑,那么這個(gè)手勢(shì)就會(huì)失效盯拱。)
在A界面:
- (void)viewDidAppear:(BOOL)animated {
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;//重新設(shè)置代理
self.navigationController.interactivePopGestureRecognizer.enabled = NO;//本頁(yè)面不能右滑
}
在B里面:
設(shè)置返回按鈕:
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頁(yè)面里面(A頁(yè)面里面的backBarButtonItem其實(shí)就是我們看到的B頁(yè)面的返回按鈕,B頁(yè)面的leftBar是左側(cè)的按鈕例嘱,注意優(yōu)先級(jí)狡逢,B里面有自定義leftBar,顯示B頁(yè)面的蝶防,如果沒(méi)有甚侣,看A界面明吩,如果里面有backBarButtonItem间学,顯示,如果沒(méi)有印荔,就是系統(tǒng)默認(rèn)的那個(gè))
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;
我采用的是方法一低葫,方法二我沒(méi)有辦法調(diào)整返回按鈕的位置