Push ViewController from Right To Left with UINavigationController

后端 未结 11 1721
名媛妹妹
名媛妹妹 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:58

    Swift 5.0

     let pageView = TaskFolderListViewController.init(nibName: "HomePageController_iPhone", bundle: nil)
     let transition = CATransition()
     transition.duration = 0.45
     transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
     transition.type = CATransitionType.push
     transition.subtype = CATransitionSubtype.fromLeft
     self.navigationController?.view.layer.add(transition, forKey: kCATransition)
     self.navigationController?.pushViewController(pageView, animated: false)
    
    0 讨论(0)
  • 2020-12-02 12:00

    This code is working fine. Please try

    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionReveal;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:phoneServicesViewController animated:NO];
    
    0 讨论(0)
  • 2020-12-02 12:05

    Please try this one

    HomePageController  *pageView = [[HomePageController alloc] initWithNibName:@"HomePageController_iPhone" bundle:nil];
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.45;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:pageView animated:NO];
    
    0 讨论(0)
  • 2020-12-02 12:06

    I had the same problem, this is how I solved it

    [self.navigationController popViewControllerAnimated:YES];
    

    This will resemble "going back" i.e a situation where a back button is clicked and you navigate to the previous page

    0 讨论(0)
  • 2020-12-02 12:08

    Based on @felixlam 's answer, I upgraded and created a "reverse" direction navigation controller that overrides the default push / pop methods to act like this.

    class LeftPushNavigationController: BaseNavigationController {
    
      override func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.isEnabled = false
      }
    
      override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // If the push is not animated, simply pass to parent
        guard animated else { return super.pushViewController(viewController, animated: animated) }
    
        // Setup back button
        do {
          // Hide original back button
          viewController.navigationItem.setHidesBackButton(true, animated: false)
          // Add new one
          viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(
            image: #imageLiteral(resourceName: "iconBackReverse"),
            style: .plain,
            target: self,
            action: #selector(self.pop)
          )
        }
    
        // Calculate final order
        let finalVCs = self.viewControllers + [viewController]
    
        // Insert the viewController to the before-last position (so it can be popped to)
        var viewControllers = self.viewControllers
        viewControllers.insert(viewController, at: viewControllers.count - 1)
    
        // Insert viewcontroller before the last one without animation
        super.setViewControllers(viewControllers, animated: false)
        // Pop with the animation
        super.popViewController(animated: animated)
        // Set the right order without animation
        super.setViewControllers(finalVCs, animated: false)
      }
    
      override func popViewController(animated: Bool) -> UIViewController? {
        // If the push is not animated, simply pass to parent
        guard animated else { return super.popViewController(animated: animated) }
    
        guard self.viewControllers.count > 1 else { return nil }
    
        // Calculate final order
        var finalVCs = self.viewControllers
        let viewController = finalVCs.removeLast()
    
        // Remove the parent ViewController (so it can be pushed)
        var viewControllers = self.viewControllers
        let parentVC = viewControllers.remove(at: viewControllers.count - 2)
    
        // Set the viewcontrollers without the parent & without animation
        super.setViewControllers(viewControllers, animated: false)
        // Create push animation with the parent
        super.pushViewController(parentVC, animated: animated)
        // Set the right final order without animation
        super.setViewControllers(finalVCs, animated: false)
    
        // Return removed viewController
        return viewController
      }
    
      @objc
      private func pop() {
        _ = popViewController(animated: true)
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题