0

When you go press '<' to pop back on your navigation controller previous screen, the screen animates by sliding to the right.

I'd like to change the back behavior so instead of it sliding to the right, it slides to the left instead.

Any ideas how I could achieve this?

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
NullHypothesis
  • 4,286
  • 6
  • 37
  • 79

1 Answers1

2

You need a custom back button. On your destination view controller create a custom UIButtonItem, set it as your leftBarButtonItem and perform an animation on button's action.

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(self.back))
}

@objc func back(){
    //Create and set up the animation
    let transition = CATransition()
    transition.duration = 0.4
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromRight//animates from right to left
    self.navigationController?.view.layer.add(transition, forKey: nil)//adds the animation
    self.navigationController?.popViewController(animated: true)
}