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
}
网友评论