Disabling segue animation

2020-06-02 08:01发布

问题:

I want to Show (e.g. push) segues in my storyboard, to connect my viewcontrollers and my navigation controller. Then the navigation bars on the viewcontrollers will show correctly. For example: With show detail or present modaly, the navigation bar will disappear

But I don't want segue animation. Xcode is giving the warning like : "Disabling segue animation is not available prior to iOS 9.0"

And I wants deployment target of iOS 7.0 or 8.0

How can I solve this?

Thanks in advance.

回答1:

You can disable animations before performing the segue and after enable it again.

UIView.setAnimationsEnabled(false)
self.performSegueWithIdentifier("next", sender: nil)
UIView.setAnimationsEnabled(true)

This will perform the segue without the animation.



回答2:

Click on segue arrow in Main.Storyboard and then:

Check out Animates



回答3:

I made a custom segue, using the Swift answer in this thread:
Push segue in xcode with no animation

So:

class ShowNoAnimationSegue: UIStoryboardSegue {

    override func perform() {
        let source = sourceViewController as UIViewController
        if let navigation = source.navigationController {
            navigation.pushViewController(destinationViewController as UIViewController, animated: false)
        }
    }
}

And in Xcode, in the Attributes Inspector of the custom Segues, I have checked the 'Animates' box (YES). Now the warning is gone, so that is why I am answering my own question.

I am not really sure yet if it is a durable solution.



回答4:

If you want to switch animate state in the code, You can duplicate your segue in the storyboard, with different identifiers, and the same origin and destination. Then make one of theme animates and the other not. Then, do performSegue with the desired identifier.

class MyNavigationController : UINavigationController {

    var firstTransitionAnimated : Bool = true // or false, based on initialization


    override func viewDidLoad() {
        super.viewDidLoad()
        var properSegue = firstTransitionAnimated ? "animated_segue" : "not_animated_segue"
        self.performSegue(withIdentifier: properSegue, sender: self)
    }
}