1自定义导航按钮 会导致返回手势失效
2自定义push 动画同样会导致返回手势失效
3自定义导航按钮同样会导致自定义 push 通话失效
以上3点其实都是委托对象改变引起的
根据以上影响关系,可以再 view 生命周期不同阶段进行先后设置就行了
viewDidLoad 中进行自定义导航按钮
viewWillAppear:(BOOL)animated中设置导航委托
self.navigationController.delegate=self;
viewDidAppear:(BOOL)animated设置 pop 手势委托
self.navigationController.interactivePopGestureRecognizer.delegate= (id)self;
这样的顺序就能完美解决同时自定义 push 通话,自定义导航按钮,还不影响返回手势
代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.leftBarButtonItem setTitle:@"返回" forState:UIControlStateNormal];
[self.rightBarButtonItem setTitle:@"完成" forState:UIControlStateNormal];
[self.rightBarButtonItem addTarget:self action:@selector(rightButton) forControlEvents:UIControlEventTouchUpInside];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.delegate=self;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.navigationController.interactivePopGestureRecognizer.delegate= (id)self;
}
-(UIButton *)leftBarButtonItem{
if (!_leftBarButtonItem) {
_leftBarButtonItem=[UIButton buttonWithType:UIButtonTypeCustom];
[_leftBarButtonItem setBackgroundColor:[UIColor redColor]];
[_leftBarButtonItem setFrame:CGRectMake(0, 0, 60, 44)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButtonItem];
[_leftBarButtonItem addTarget:self action:@selector(leftBarButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _leftBarButtonItem;
}
-(void)leftBarButtonAction{
[self.navigationController popViewControllerAnimated:YES];
}
-(UIButton *)rightBarButtonItem{
if (!_rightBarButtonItem) {
_rightBarButtonItem=[UIButton buttonWithType:UIButtonTypeCustom];
[_rightBarButtonItem setBackgroundColor:[UIColor redColor]];
[_rightBarButtonItem setFrame:CGRectMake(0, 0, 60, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButtonItem];
}
return _rightBarButtonItem;
}
网友评论