Reputation: 174
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
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