Push ViewController from Right To Left with UINavigationController

后端 未结 11 1720
名媛妹妹
名媛妹妹 2020-12-02 11:27

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

相关标签:
11条回答
  • 2020-12-02 11:44

    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.

    0 讨论(0)
  • 2020-12-02 11:45

    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.

    0 讨论(0)
  • 2020-12-02 11:49
    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];
    
    0 讨论(0)
  • 2020-12-02 11:52

    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];
    
    0 讨论(0)
  • 2020-12-02 11:53

    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

    0 讨论(0)
  • 2020-12-02 11:55

    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)
            }
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题