As everyone know the UINavigationController push a ViewController from Left To Right, is there a way to push the View from Right To Left? like the animation for the back but
Just an improvement of the best answer here in my opinion:
UIViewController *newVC = ...;
[self.navigationController setViewControllers:@[newVC, self] animated:NO];
[self.navigationController popViewControllerAnimated:YES];
And to go back to the initial controller, just do a push to it.
No, right to left is reserved for popping viewcontrollers from the navigation stack. You can however get such an effect by animating the views yourself. Something like:
[UIView beginAnimations:@"rightToLeft" context:NULL];
CGRect newFrame = aView.frame;
newFrame.origin.x -= newFrame.size.width;
aView.frame = newFrame;
[UIView commitAnimations];
This will however not do anything to your navigation controller.
Try this :
//Push effect in reverse way
CATransition* transition = [CATransition animation];
transition.duration = 0.75;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:vc animated:NO];
You can create a NSMutableArray
from the navigationController's array of viewcontrollers
and insert new viewController before the current one. Then set the viewControllers array without animation and pop back.
UIViewController *newVC = ...;
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:newVC atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];
You seem to want to "pop back". This can be achieved by three methods on your UINavigationController
instance:
To come back to the "root" controller:
-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated
Or to come back to one of the previous controllers :
-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
Or to come back to the previously pushed controller :
-(UIViewController *)popViewControllerAnimated:(BOOL)animated
Based on previous best answers, you can make UINavigationController
extension and preserve the navigation stack as follows:
extension UINavigationController {
/// Pushes view controller into navigation stack with backwards animation.
func pushBackwards(viewController newViewController: UIViewController) {
if let currentController = viewControllers.last {
let previousControllers = viewControllers[0..<viewControllers.endIndex-1]
var controllers = Array(previousControllers + [newViewController, currentController])
setViewControllers(controllers, animated: false)
popViewController(animated: true)
// Adjusting the navigation stack
_ = controllers.popLast()
controllers.insert(currentController, at: controllers.count-1)
setViewControllers(controllers, animated: false)
} else {
// If there is no root view controller, set current one without animation.
setViewControllers([newViewController], animated: false)
}
}
}