美文网首页
swift 自定义导航栏 右滑返回 UICollectionVi

swift 自定义导航栏 右滑返回 UICollectionVi

作者: 喵喵粉 | 来源:发表于2020-04-14 10:42 被阅读0次
  1. 自定义导航栏,右滑返回失效

方法:
重写UIGestureRecognizerDelegate代理的方法,设置interactivePopGestureRecognizerdelegate

func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) 
import UIKit

class BaseNavVC: UINavigationController {

    var popDelegate: UIGestureRecognizerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        ///pop
        self.popDelegate = self.interactivePopGestureRecognizer?.delegate
        self.delegate = self
    }
}

//
// MARK: - pop手势
//
extension BaseNavVC: UINavigationControllerDelegate {
    
    //MARK: - UIGestureRecognizerDelegate代理
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        
        //实现滑动返回的功能
        //清空滑动返回手势的代理就可以实现
        if viewController == self.viewControllers[0] {
            interactivePopGestureRecognizer?.delegate = self.popDelegate
        } else {
            interactivePopGestureRecognizer?.delegate = nil
        }
    }
    
        
    //    override var preferredStatusBarStyle: UIStatusBarStyle {
    //        return .lightContent
    //    }
}

//
// MARK: - 自定义导航栏
//
extension BaseNavVC {
    
    fileprivate func setupUI() {
        /// 设置导航栏标题颜色
        //navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.ext3F3F3F]

        /// 系统的左右rightBarButtonItem的颜色
        navigationBar.tintColor = UIColor.white
            
        ///去掉底部线条
        //navigationBar.setBackgroundImage(UIImage(), for: .default)
        /// 导航栏背景色
        navigationBar.barTintColor = UIColor.extSelectColor
        navigationBar.shadowImage = UIImage()

        //导航栏背景色 (颜色有偏差,是因为有一层毛玻璃效果视图 取消毛玻璃效果)  这个会影响subview的坐标子控制器视图整体下移
        navigationBar.isTranslucent = false
        // 视图延伸不考虑透明的Bars(这里包含导航栏和状态栏) 意思就是延伸到边界
        extendedLayoutIncludesOpaqueBars = false
    }

    
    //拦截跳转事件
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if children.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            //添加图片
            let image = UIImage(named: "arrowLargeLeft")?.extChangeWith(color: .ext3F3F3F)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(leftClick))
        }
        super.pushViewController(viewController, animated: animated)
        
    }
    
    //返回上一层控制器
    @objc func leftClick()  {
        popViewController(animated: true)
    }
}
  1. 页面有UICollectionView分页,右滑返回失效
image.png

方法:
UICollectionView将与右滑手势起冲突的手势禁用

oc

NSArray *gestures = self.navigationController.view.gestureRecognizers;

for (UIGestureRecognizer *gesture in gestures) {
    if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        [scrollView.panGestureRecognizer requireGestureRecognizerToFail: gesture];
    }
}

swift

if let gestures = navigationController?.view.gestureRecognizers {
    for gesture in gestures {
        if gesture is UIScreenEdgePanGestureRecognizer {
            cvClass.panGestureRecognizer.require(toFail: gesture)
        }
    }
}

相关文章

网友评论

      本文标题:swift 自定义导航栏 右滑返回 UICollectionVi

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