E. Huckabee
E. Huckabee

Reputation: 1818

Present New ViewController programmatically [Swift 3]

What I am trying to do is present a new view controller programmatically with Swift 3 with an animated fade-out then fade-in and a pop-up then pop-down depending on what view controllers the user enters. This is so my app feels more modern and less old and blocky.

Here is the current method I am using for presenting a new view controller. It works, but it is rather abrupt and robotic:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let ExampleVC = storyBoard.instantiateViewController(withIdentifier: "ExampleVC")
//ExampleVC standing for ExampleViewController 
self.navigationController?.pushViewController(ExampleVC, animated: false) 

here is a method for animating UIViews but it doesnt work with UIViewControllers

func popIn(yourView : UIView) {

        yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
            yourView.transform = .identity
        }, completion: nil)
        self.view.addSubview(yourView)
    }

i need something similar to this but for a UIViewController

UPDATE

here is what you would use to to present a new viewcontroller in a cool way to make your app more modern

let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let VC = storyboard.instantiateViewController(withIdentifier: "Example") //<<< make sure you enter in your storyboard ID here or you will crash your app
    VC.modalTransitionStyle = .crossDissolve //you can change this to do different animations
    VC.view.layer.speed = 0.1 //adjust this to animate at different speeds
    self.navigationController?.present(VC, animated: true, completion: nil)

if you have any questions comment them so i can help you guys out

Upvotes: 1

Views: 2271

Answers (1)

cole
cole

Reputation: 3357

var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ExampleVC")
ivc.modalTransitionStyle = .crossDissolve
ivc.view.layer.speed = 0.1
self.present(ivc, animated: true, completion: { _ in })

Check this code for animation to view controller

Upvotes: 4

Related Questions