NullHypothesis
NullHypothesis

Reputation: 4506

Change screen animation when back button is pressed

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?

Upvotes: 0

Views: 1040

Answers (1)

Williamberg Farias
Williamberg Farias

Reputation: 105

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)
}

Upvotes: 2

Related Questions