向后滑动添加动作swift 4 [英] Adding an action to back swipes swift 4
问题描述
我已将其中一个视图控制器的导航栏中的后退按钮更改为自定义按钮,并按下了该按钮即可执行该操作:
I have changed the back button in my navigation bar in one of my view controllers to a custom button and gave that button an action to do when pressed by:
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: UIImage(named: "backArrow"), style: .plain, target: self, action: #selector(self.back))
self.navigationItem.leftBarButtonItem = backButton
这很好用,我可以使用以下方法保持向后滑动以弹出视图控制器:
This worked perfectly and I was able to keep the back swipe to pop the view controller by using:
self.interactivePopGestureRecognizer?.isEnabled = true
self.interactivePopGestureRecognizer?.delegate = self
在我的导航控制器文件中.不幸的是,当我向后滑动时,我无法(或者我还没有弄清楚)调用视图控制器中的back()
函数来更改后退按钮.我如何以及在哪里可以检测到要向后滑动,以便可以在该特定视图控制器中调用back函数?
in my navigation controller file. Unfortunately, when I back swipe, I can not (or I haven't figured out) call on the back()
function in my view controller that changes the back button. How and where can I detect that I am swiping to go back so I can call on the back function in that specific view controller?
推荐答案
就像这个答案,您可以订阅手势: https://stackoverflow.com/a/36893464/1484378
Like this answer you can just subscribe to the gesture: https://stackoverflow.com/a/36893464/1484378
// In UINavigationController
override func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer?.addTarget(self, action:#selector(self.handlePopGesture))
}
@objc func handlePopGesture(gesture: UIGestureRecognizer) -> Void {
if gesture.state == .began {
back()
}
}
您可能必须添加一些自定义逻辑来访问视图控制器的back方法,例如:if let vc = visibleViewController as? MyViewController { vc.back() }
You will probably have to add some custom logic to access your view controller's back method like: if let vc = visibleViewController as? MyViewController { vc.back() }
这篇关于向后滑动添加动作swift 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!