Dor Cohen
Dor Cohen

Reputation: 17080

Present ViewController on top of other programmatically

I have the following code which present my viewcontroller instead of an existing one:

let frameworkBundle = NSBundle(identifier: "me.app.MyFramework")
var storyBoard:UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: frameworkBundle)

var vc = storyBoard.instantiateInitialViewController() as MyPresenterViewController

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)

But I want to present the viewcontroller on top of the other one programmatically,

any ideas?

Upvotes: 16

Views: 28838

Answers (3)

Nikita Kukushkin
Nikita Kukushkin

Reputation: 15138

If you want your modal view controller to be see-though you have to give its view a clear color background. And set its modalPresentationStyle to .OverCurrentContext.

let vc = storyBoard.instantiateInitialViewController() as! MyPresenterViewController
vc.view.backgroundColor = .clearColor()
vc.modalPresentationStyle = .OverCurrentContext

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)

Upvotes: 41

Dor Cohen
Dor Cohen

Reputation: 17080

I used the following approach in order to present my view only on the bottom of the other view controller:

var presenterStoryBoard:UIStoryboard = UIStoryboard(name: "MiniAppPanel", bundle: frameworkBundle)

var vc = presenterStoryBoard.instantiateInitialViewController() as MiniAppPanelViewController

vc.view.frame = CGRectMake(0, vc.view.frame.size.height - 120, vc.view.frame.size.width, 120)

publisherViewController.addChildViewController(vc)    
publisherViewController.view.addSubview(vc.view)

Upvotes: 6

Thellimist
Thellimist

Reputation: 4007

If your using navigationcontroller you can change viewcontrollers as

    navigationController?.presentViewController(newVC, animated: true, completion: nil)

but since presenting doesn't bring the view from left or right you'll lose your navigationcontroller

Upvotes: 1

Related Questions