Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sbhmajd/54c4317776dea674b2e94319020557a5 to your computer and use it in GitHub Desktop.
Save sbhmajd/54c4317776dea674b2e94319020557a5 to your computer and use it in GitHub Desktop.
Completion handler for UINavigationController push or pop
/// https://stackoverflow.com/a/33767837
/// https://iganin.hatenablog.com/entry/2019/07/27/172911
extension UINavigationController {
public func pushViewController(
_ viewController: UIViewController,
animated: Bool,
completion: @escaping () -> Void) {
pushViewController(viewController, animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
public func popViewController(
animated: Bool,
completion: @escaping () -> Void) {
popViewController(animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
public func popToViewController(
_ viewController: UIViewController,
animated: Bool,
completion: @escaping () -> Void) {
popToViewController(viewController, animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
}
#if DEBUG
import UIKit
extension UIWindow {
// WARNING: UIWindowのモーションジェスチャーを奪ってるので、利用する場合は要対応
override open func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
guard motion == .motionShake else { return }
let vc = UIViewController()
guard vc.parent == nil,
rootViewController?.presentedViewController == nil
else { return }
let navi = UINavigationController(rootViewController: vc)
rootViewController?.present(navi, animated: true, completion: nil)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment