美文网首页
控制器侧滑返回修复

控制器侧滑返回修复

作者: 牛1688 | 来源:发表于2019-12-15 00:02 被阅读0次

    importUIKit

    class twoVC: UIViewController {

        lazyvarbackButton:UIButton= {

             var backButton = UIButton.init(type: UIButton.ButtonType.custom)

                 backButton.setTitle("返回", for:UIControl.State.normal)

                backButton.frame.size=CGSize.init(width:UIScreen.main.bounds.width, height:40)

                backButton.frame.origin=CGPoint.init(x:0, y:80)

                 backButton.addTarget(self, action:#selector(backAcion), for:UIControl.Event.touchUpInside)

            backButton.backgroundColor = UIColor.blue

            backButton.titleLabel?.textAlignment = .left

             returnbackButton

         }()

        var navigationDelegete : UIGestureRecognizerDelegate!

        override func viewDidLoad() {

            super.viewDidLoad()

            initSet()

            view.backgroundColor = UIColor.orange

        }

        /// 手势设置代理放在这里,否则侧滑过程中方弃操作, viewWillDisappear会调用,再回来时手势说失效了

        overridefuncviewWillAppear(_animated:Bool) {

            super.viewWillAppear(animated)

            ///保存上层控制器代理

            self.navigationDelegete = self.navigationController?.interactivePopGestureRecognizer?.delegate

            /// 设置新导航控制器代理为自己

            self.navigationController?.interactivePopGestureRecognizer?.delegate = self

            /// 侧滑时不导航条不闪烁

            self.navigationController?.setNavigationBarHidden(true, animated: true)

        }

        overridefuncviewWillDisappear(_animated:Bool) {

            super.viewWillDisappear(animated)

            ///恢复导航控制器代理

            self.navigationController?.interactivePopGestureRecognizer?.delegate = self.navigationDelegete

            /// 侧滑时不导航条不闪烁

            self.navigationController?.setNavigationBarHidden(false, animated: true)

        }

        @objcfuncbackAcion()  {

            navigationController?.popViewController(animated: true)

        }

        funcinitSet() {

            view.addSubview(backButton)

            title="\(self.self)"

        }

    }

    extension UIViewController:UIGestureRecognizerDelegate{

        /// 允许手势 开始识别

        publicfuncgestureRecognizerShouldBegin(_gestureRecognizer:UIGestureRecognizer) ->Bool{

            return true

        }

        /// 允许多个手势,识别

        publicfuncgestureRecognizer(_gestureRecognizer:UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:UIGestureRecognizer) ->Bool{

               returntrue

        }

    }

    相关文章

      网友评论

          本文标题:控制器侧滑返回修复

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