0
votes

So I have 2 View Controllers and a Navigation Controller. When the screen is clicked, the 1st VC segues to the 2nd VC, and there is a back button for the segue to unwind to go back to the 1st VC.

I did not like the vertical animation of the segues, so (with some help) I created custom, horizontal animations.

The 1st VC works great with the animation sliding from right to left. But once that is done, the 2nd VC does not want to unwind (2nd VC should go from left to right).

I do get this Warning..

Warning: Attempt to present UINavigationController: 0x7fce2082b000 on TestApp.HomeViewController: 0x7fce20410030 whose view is not in the window hierarchy!

Also, if I take the script from the 1st VC segue, then I am able go unwind from the 2nd VC w/ the proper animation.

Here's the code for the segues:

1st VC

@IBAction func performSegue(_ sender: Any) {

    if shouldPerformSegue(withIdentifier: "segue", sender: Any?.self) {

        performSegue(withIdentifier: "segue", sender: nil)
    }
}

@IBAction func unwindToHomeView(segue: UIStoryboardSegue) {

}

override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
    let segue = SegueFromLeft(identifier: unwindSegue.identifier, source: unwindSegue.source, destination: unwindSegue.destination)
    segue.perform()
}

Right to Left Animation

    let dst = self.destination
    let src = self.source

    let containerView = src.view.superview

    dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)

    containerView?.addSubview(dst.view)

    UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
    }, completion: { success in
        src.present(dst, animated: false, completion: nil)
    })

Left to Right Animation

    let dst = self.destination
    let src = self.source

    src.view.superview?.insertSubview(dst.view, at: 0)

    UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
        src.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)
    }, completion: { success in
        src.dismiss(animated: false, completion: nil)
    })

Any help would be greatly appreciated.

2
So.. I did away with the animations. The solution to my animation was fairly simple. Just switching around the position of the navigator, and having it as the initial view controller did the trick. Fixing any animation or pushing problems.Slavic the Slavic
Not sure if I should put my solution down, since it does not really help answer my specific question.Slavic the Slavic

2 Answers

1
votes

You can simply push viewcontroller to navigation controllers where you don't need to explicitly handle left right animation. In VC1 navigationController?.pushViewController(VC2, animated: true) will simply animate VC2 from right to left with handy back button which can be used to animate VC2 left to right displaying VC1.

0
votes

For my solution I actually did away with any "custom" animations, and fixed my issues mostly just by moving the navigation controller as the initial controller, attached to the VC1.

From there I just pushed to the next view (VC2), and used unwind segue to go back (to VC1).

Basic code for pushing:

let vcName = "Main"
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)