0
votes

I have 2 controllers:

  • First view controller (1)
  • Second view controller (2) embed in Navigation controller (2a)

( 1 ) -> ( 2a - 2 )

And custom Segue navigation between VC:

  • normal CustomSegue. From 1 to 2
  • unwind CustomUnwindSegue. From 2 to 1

When I use normal segue: Second controller (including navigation bar!!!) push First controller from right side

When I use unwind segue: First controller push Second controller (without navigation bar!!! Only view inside Navigation controller) to right side. Navigation bar is sticky! When navigation completed bar disappears.

How can I push View Controller with Navigation bar.

My code:

class CustomSegue: UIStoryboardSegue {

    override func perform() {

        let toViewController: UIViewController = self.destinationViewController as! UIViewController
        let fromViewController: UIViewController = self.sourceViewController as! UIViewController

        let containerView: UIView? = fromViewController.view.superview
        let screenBounds: CGRect = UIScreen.mainScreen().bounds

        let finalToFrame: CGRect = screenBounds
        let finalFromFrame: CGRect = CGRectOffset(finalToFrame, -screenBounds.size.width, 0)

        toViewController.view.frame = CGRectOffset(finalToFrame, screenBounds.size.width, 0)
        containerView?.addSubview(toViewController.view)

        UIView.animateWithDuration(0.5, animations: {

            toViewController.view.frame = finalToFrame
            fromViewController.view.frame = finalFromFrame

            }, completion: { finished in
                let fromVC: UIViewController = self.sourceViewController as! UIViewController
                let toVC: UIViewController = self.destinationViewController as! UIViewController
                fromVC.presentViewController(toVC, animated: false, completion: nil)
        })
    }
}

class CustomUnwindSegue: UIStoryboardSegue {

    override func perform() {

        let toViewController: UIViewController = self.destinationViewController as! UIViewController
        let fromViewController: UIViewController = self.sourceViewController as! UIViewController

        let containerView: UIView? = fromViewController.view.superview
        let screenBounds: CGRect = UIScreen.mainScreen().bounds

        let finalToFrame: CGRect = screenBounds
        let finalFromFrame: CGRect = CGRectOffset(finalToFrame, screenBounds.size.width, 0)

        toViewController.view.frame = CGRectOffset(finalToFrame, -screenBounds.size.width, 0)
        containerView?.addSubview(toViewController.view)

       UIView.animateWithDuration(0.5, animations: {

            toViewController.view.frame = finalToFrame
            fromViewController.view.frame = finalFromFrame

            }, completion: { finished in
                let fromVC: UIViewController = self.sourceViewController as! UIViewController
                let toVC: UIViewController = self.destinationViewController as! UIViewController
                fromVC.dismissViewControllerAnimated(false, completion: nil)
        })
    }
}
1

1 Answers

0
votes

In the forward segue, you are animating from VC1 to VC2a, which will be the destination of the segue. Animating VC2a in also makes VC2 appear, because VC2a presents it as part of its view. So the UINavigationBar and VC2 both slide in.

In the reverse segue, you are animating from VC2 to VC1. VC2's view does not cover the whole screen, because some is taken up by the UINavigationBar. You animate VC2's view to the side and replace it with VC1, and when that is finished, you dismissViewControllerAnimated which causes VC2a to disappear without animation.

Instead of animating VC2's view in the reverse segue, you need to animate VC2a's view again. This should carry VC2 along with it. I haven't tried this code, but something like this:

   UIView.animateWithDuration(0.5, animations: {
        toViewController.view.frame = finalToFrame
        VC2a.view.frame = finalFromFrame
   }