Skip to content

Instantly share code, notes, and snippets.

@saito-sv
Last active September 23, 2021 17:03
Show Gist options
  • Save saito-sv/9b58c4f55e2ab81dc68c7b9929939519 to your computer and use it in GitHub Desktop.
Save saito-sv/9b58c4f55e2ab81dc68c7b9929939519 to your computer and use it in GitHub Desktop.
iOS reverse push and pop navigation swift 3
import UIKit
class ResersableNav: UINavigationController {
var controllersCount:Int {
return reversedPushControllers.count + viewControllers.count
}
var reversedPushControllers:[UIViewController] = []
func reversePush(controller:UIViewController, animated:Bool) {
var controllers = self.viewControllers
reversedPushControllers.append(controllers.last!)
controllers.insert(controller, at: controllers.count - 1)
setViewControllers(controllers, animated: false)
popViewController(animated: animated)
}
func reversePop(animated:Bool) {
var controllers = self.viewControllers
controllers.removeLast()
controllers.append(reversedPushControllers.last!)
reversedPushControllers.removeLast()
setViewControllers(controllers, animated: animated)
}
func setRoot(controller:UIViewController) {
if controllersCount > 1 {
if reversedPushControllers.count != 0 {
reversedPushControllers[0] = controller
popToRoot(animated: true)
}else {
var viewControllers = self.viewControllers
viewControllers[0] = controller
setViewControllers(viewControllers, animated: false)
popToRoot(animated: true)
}
}else if topViewController != controller {
UIView.animate(withDuration: 0.4, delay: 0, options: [.allowAnimatedContent, .transitionCrossDissolve], animations: {
UIView.setAnimationsEnabled(false)
}, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.isEnabled = false
}
@discardableResult func popToRoot(animated:Bool) ->[UIViewController]?{
if reversedPushControllers.count != 0 {
setViewControllers([reversedPushControllers[0]], animated: animated)
}else {
return super.popToRootViewController(animated: animated)
}
reversedPushControllers.removeLast()
return nil
}
}
@Dougly
Copy link

Dougly commented Dec 14, 2017

Thank you for this!

@saito-sv
Copy link
Author

Thank you for this!

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment