我有一个关于取消 UIViewController 中后退按钮触发的后退事件的问题。在 Objective-C 中有以下内容扩大 https://github.com/onegray/UIViewController-BackButtonHandler。我真的不知道如何将其转换为 swift。我到目前为止尝试的是用我自己的函数覆盖 backBarButton 但它不起作用:
navigation.backBarButtonItem?.action = #selector(MyController.back)
navigation.backBarButtonItem?.target = self
我搜索了诸如委托函数之类的东西,但找不到后退按钮的任何内容。
当我遇到这个问题时,我重写了这个扩大 https://github.com/onegray/UIViewController-BackButtonHandler到斯威夫特 3
该解决方案使系统后退按钮带有“
public protocol VCWithBackButtonHandler {
func shouldPopOnBackButton() -> Bool
}
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if viewControllers.count < (navigationBar.items?.count) ?? 0 {
return true
}
var shouldPop = true
let vc = self.topViewController
if let vc = vc as? VCWithBackButtonHandler {
shouldPop = vc.shouldPopOnBackButton()
}
if shouldPop {
DispatchQueue.main.async {[weak self] in
_ = self?.popViewController(animated: true)
}
} else {
for subView in navigationBar.subviews {
if(0 < subView.alpha && subView.alpha < 1) {
UIView.animate(withDuration: 0.25, animations: {
subView.alpha = 1
})
}
}
}
return false
}
}
Usage:
class ViewController: UIViewController,VCWithBackButtonHandler{
public func shouldPopOnBackButton() -> Bool {
return false
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)