Sunday, January 18, 2015

Overview

So you have a ViewController in your storyboard and you want to be able to display it from code but don’t know how. The answer is the presentViewController function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// we have to get the controller
// Make sure you add an Identified to your controller
// in the storyboard
var newController = self.storyboard?.instantiateViewControllerWithIdentifier("NewViewController") as NewViewController

// If you want to fade out or animate your current viewcontroller
// you have to use a CATransition
var transition = CATransition()
transition.duration = 1;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromBottom;
self.view.window?.layer.addAnimation(transition, forKey: kCATransition)

// Now we display our new controller
// The animation is false because the default animation is bad
self.presentViewController(newController, animated: false, completion: nil)

Random Posts