UINavigationController滑動(dòng)返回

UINavigationController全屏滑動(dòng)返回

//注銷系統(tǒng)方法
   self.interactivePopGestureRecognizer.enabled = NO;

// 創(chuàng)建全屏滑動(dòng)手勢,調(diào)用系統(tǒng)自帶滑動(dòng)手勢的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];

// 設(shè)置手勢代理,攔截手勢觸發(fā) 
// 聲明UIGestureRecognizerDelegate 代理
    pan.delegate = self;

   // 給導(dǎo)航控制器的view添加全屏滑動(dòng)手勢
    [self.view addGestureRecognizer:pan];
// 什么時(shí)候調(diào)用:每次觸發(fā)手勢之前都會詢問下代理,是否觸發(fā)。
// 作用:攔截手勢觸發(fā)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 注意:只有非根控制器才有滑動(dòng)返回功能,根控制器沒有。
    // 判斷導(dǎo)航控制器是否只有一個(gè)子控制器,如果只有一個(gè)子控制器,肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用戶在根控制器界面,就不需要觸發(fā)滑動(dòng)手勢,
        return NO;
    }
    return YES;
}

UINavigationController左邊緣滑動(dòng)返回

// 為了獲取保存系統(tǒng)的 代理
@property (nonatomic ,assign) id target;
// 獲取系統(tǒng)自帶滑動(dòng)手勢的target對象
    self.target = self.interactivePopGestureRecognizer.delegate;
// 實(shí)現(xiàn)UINavigationControllerDelegate的代理和聲明 
    self.delegate = self;
//UINavigationControllerDelegate 代理方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (viewController == [self.viewControllers firstObject] )
    {
        self.interactivePopGestureRecognizer.delegate = self.target;
    }
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count>0)
    {
       // 設(shè)置導(dǎo)航條左邊按鈕的內(nèi)容,把系統(tǒng)的返回按鈕給覆蓋,導(dǎo)航控制器的滑動(dòng)返回功能就消失了 
       //覆蓋后如果不把系統(tǒng)滑動(dòng)返回功能注銷 或自己重新實(shí)現(xiàn) 在首界面左邊緣滑動(dòng)會崩潰
     // 注銷方法self.interactivePopGestureRecognizer.delegate = nil;
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
        button.size = button.currentImage.size;
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationBarHidden=NO;
        self.interactivePopGestureRecognizer.delegate = nil;
        viewController.automaticallyAdjustsScrollViewInsets=NO;
    }
    [super pushViewController:viewController animated:animated];
}
-(void)backButtonClick
{
    [self popViewControllerAnimated:YES];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容