美文网首页
ios 自定义导航按钮或自定义 push 动画,返回手势失效

ios 自定义导航按钮或自定义 push 动画,返回手势失效

作者: FSDemo | 来源:发表于2017-04-11 15:38 被阅读0次

    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;
    }
    

    相关文章

      网友评论

          本文标题:ios 自定义导航按钮或自定义 push 动画,返回手势失效

          本文链接:https://www.haomeiwen.com/subject/zzfuattx.html