Kristian H
Kristian H

Reputation: 174

How to change animation direction on pushViewController on leftBarButtonItem?

I've added a leftBarButtonItem to a NavigationBar(in conjunction with NavigationController). The leftBarButtonItem is on the top ViewController.

When I use the pushViewController func to go the next ViewController, the animation is from right to left, the same as when pressing a button on the right side of the navigationBar.

How can I change the animation direction to go from left to right, same as for example in Tinder when pressing the left button on the navigation bar?

Here's the code:

let profileButton = UIBarButtonItem(image: #imageLiteral(resourceName: "profile"), style: .plain, target: self, action: #selector(self.showProfile))
    self.navigationItem.leftBarButtonItem = profileButton

@objc func showProfile() {
    let profileViewController = storyboard?.instantiateViewController(withIdentifier: "profile") as! ProfileViewController
    self.navigationController?.pushViewController(profileViewController, animated: true)
}

Upvotes: 2

Views: 1156

Answers (2)

Vinu Jacob
Vinu Jacob

Reputation: 381

To animate while push to another viewController, you can use CATransition

@objc func showProfile() {
    let profileViewController = storyboard?.instantiateViewController(withIdentifier: "profile") as! ProfileViewController
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.push
    transition.subtype = CATransitionSubtype.fromLeft       
    navigationController?.view.layer.add(transition, forKey: kCATransition)
    self.navigationController?.pushViewController(profileViewController, animated: true)
}

You can change the animation direction as fromRight, fromTopfromBottom

Upvotes: 3

Tal Cohen
Tal Cohen

Reputation: 1457

You cannot change the direction of UINavigationController. In order to achieve that, you'll have to create it on your own. If you want to implement a menu, you can check out SideMenu.

Upvotes: 0

Related Questions