-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* β¨ Add BackGesture helper * π Hide import behind compiler directive * π Update CHANGELOG.md
- Loading branch information
1 parent
99b74ba
commit 31e80fa
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#if os(iOS) | ||
import UIKit | ||
|
||
private class BackGestureDelegate: NSObject { | ||
// MARK: - Private properties | ||
|
||
private weak var navigationController: UINavigationController? | ||
|
||
// MARK: Initializers | ||
|
||
init( | ||
navigationController: UINavigationController | ||
) { | ||
super.init() | ||
self.navigationController = navigationController | ||
} | ||
} | ||
|
||
// MARK: - UIGestureRecognizerDelegate | ||
extension BackGestureDelegate: UIGestureRecognizerDelegate { | ||
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { | ||
navigationController.map { $0.viewControllers.count > 1 } ?? false | ||
} | ||
} | ||
|
||
extension UINavigationController { | ||
private enum Keys { | ||
static var backGestureDelegate: UInt8 = 0 | ||
} | ||
|
||
private var backGestureDelegate: BackGestureDelegate { | ||
if let delegate = objc_getAssociatedObject(self, &Keys.backGestureDelegate) as? BackGestureDelegate { | ||
return delegate | ||
} | ||
|
||
let delegate = BackGestureDelegate(navigationController: self) | ||
objc_setAssociatedObject( | ||
self, | ||
&Keys.backGestureDelegate, | ||
delegate, | ||
.OBJC_ASSOCIATION_RETAIN | ||
) | ||
return delegate | ||
} | ||
|
||
/// Add custom interactivePopGestureRecognizer delegate so it works even with hidden navigationBar | ||
public func setupCustomBackGestureDelegate() { | ||
assert(isViewLoaded, "View needs to be already loaded") | ||
assert( | ||
interactivePopGestureRecognizer != nil, | ||
"Cannot install gesture delegate on nil recognizer" | ||
) | ||
interactivePopGestureRecognizer?.delegate = backGestureDelegate | ||
} | ||
} | ||
#endif |