1

I'm creating an iOS app with Swift and I'd like to create custom segue which transitions from right to left. The default transition way is from left to right but I do need to have the opposite way of transition. I just wrote the code like this.

class RightToLeftSegue: UIStoryboardSegue {

    override func perform() {
        let sourceViewController = self.sourceViewController 
        let destinationViewController = self.destinationViewController

        let window = UIApplication.sharedApplication().keyWindow
        window!.rootViewController = destinationViewController
        window!.rootViewController = sourceViewController
        UIView.transitionWithView(window!, duration: 0.5, options: .TransitionFlipFromLeft, animations: { window!.rootViewController = destinationViewController }, completion: { (finished: Bool) in })
    }
}

But this just transitions flipping move. Any way I can do this? Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dave
  • 405
  • 4
  • 17

1 Answers1

3

Try this

class RightToLeftSegue : UIStoryboardSegue {

     override func perform() {

         guard let navigationController = sourceViewController.navigationController else { return }

         sourceViewController.view.superview?.addSubview(destinationViewController.view)

         let frame = sourceViewController.view.frame
         destinationViewController.view.frame = CGRectOffset(frame, -frame.size.width, 0.0)

         UIView.animateWithDuration(0.5,
         animations: {
             self.sourceViewController.view.frame = CGRectOffset(frame, frame.size.width, 0.0)
             self.destinationViewController.view.frame = frame
         })
         { (finished: Bool) in 
             navigationController.rootViewController = self.destinationViewController
         }

    }

}

EDIT

Or this!

class RightToLeftSegue : UIStoryboardSegue {

    override func perform() {
        sourceViewController.navigationController?.viewControllers = [destinationViewController, sourceViewController]
        sourceViewController.navigationController?.popViewControllerAnimated(true)
    }

}
rolling_codes
  • 15,174
  • 22
  • 76
  • 112
  • Awesome! It works! I have one question. when animation happens it shows blank page before animation completes, in this case it turns black out for a while so I changed window.backgoundColor to white. But is there any way that I can show destinationViewController as normal segue does? – Dave Apr 20 '16 at 06:19
  • @Dave Not sure what your question is. So after the animation, destinationViewController is blank instead of what you wanted? – rolling_codes Apr 20 '16 at 06:21
  • no, destinationViewController actually shows up after the animation is completed, but while animation happens it shows just blank page. – Dave Apr 20 '16 at 06:23
  • And when you say right to left... Do you mean the destination view is to the right of the source view? – rolling_codes Apr 20 '16 at 06:34
  • It didn't work.. But it's already good and close enough to my original thought. Thanks much for your help. – Dave Apr 20 '16 at 09:51
  • @Dave Let me run the code on my desktop when I get the chance. I wrote that code by hand and Swift is a little tricky to code in when not in the Xcode environment – rolling_codes Apr 20 '16 at 12:10
  • Great! Thanks much @ThomMorgan ! – Dave Apr 20 '16 at 12:12
  • @Dave try it again now. Still have not run it through Xcode though – rolling_codes Apr 20 '16 at 12:12
  • Actually when you use the segue from UITableView, it seems animation doesn't work. Do you have any idea why? – Dave Apr 28 '16 at 15:22
  • @Dave A UITableView is not a UIViewController you need to do the transition with a UITableViewController – rolling_codes Apr 28 '16 at 15:50
  • I see. In my case it's UIViewController which contains UITableView so I think it should work in the way you wrote but it didn't. – Dave Apr 28 '16 at 15:54
  • @Dave can you post the code? Are you adding the table view to the UIViewController's view object? And are you setting the dimensions of the table view or adding autolayout constraints? Append it as an edit to your question – rolling_codes Apr 28 '16 at 15:55
  • `class SampleVC: UIViewController, UITableViewDelegate, UITableViewDataSource { ` `var tableView: UITableView!` `override func viewDidLoad() {` `tableView = UITableView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))` `self.view.addSubview(tableView)` `tableView.delegate = self` `tableView.dataSource = self` `}` `...` `func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {` `self.performSegueWithIdentifier("segueIdentifier", sender: self)` `}` `}` – Dave Apr 28 '16 at 15:59
  • @Dave what do you mean by the animation does not work? If the table view has no cells in it, then you'll just see the transition to an empty table view – rolling_codes Apr 28 '16 at 16:02
  • if I setup the segue to Push(not Custom) it just shows up animatedly. but when I set the segue to Custom(in this case RightToLeftSegue class) it shows up just no animation. just as if pushing VC with animation false. `self.presentViewController(navigationController, animated: false, completion: nil)` – Dave Apr 28 '16 at 16:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110557/discussion-between-thom-morgan-and-dave). – rolling_codes Apr 28 '16 at 17:17
  • I extended my answer to support the functionality you are looking for. – rolling_codes May 02 '16 at 20:41
  • @Dave My pleasure to help! The first one had issues with the extended edge layout but gad it worked – rolling_codes May 03 '16 at 13:35