Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present ViewController on top of other programmatically

Tags:

ios

swift

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?

like image 378
Dor Cohen Avatar asked Mar 08 '15 17:03

Dor Cohen


2 Answers

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)
like image 97
Nikita Kukushkin Avatar answered Sep 17 '22 20:09

Nikita Kukushkin


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)
like image 41
Dor Cohen Avatar answered Sep 16 '22 20:09

Dor Cohen