16

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?

Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • This code seems to actually be presenting one view controller on top of another. What do you mean when you say "on top" and how does it differ from "instead"? – Nikita Kukushkin Mar 08 '15 at 17:40
  • @nkukushkin My view controller contains only a button and this is what I see, I can't see the article that under it – Dor Cohen Mar 08 '15 at 17:41
  • if you create a new window on e.g. alert-level and add your controller to that, and when you make that key, then it will top of everything on the screen, including the current keyboard as well. – holex Mar 09 '15 at 16:33

3 Answers3

41

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)
Nikita Kukushkin
  • 14,648
  • 4
  • 37
  • 45
  • I need to be also touchable, meaning that I can press on the view controller behind, is it possible? – Dor Cohen Mar 09 '15 at 10:34
  • I didn't understood how I can override hittest, it's a method of my UIVIEW meaning self.view. How can I override it? – Dor Cohen Mar 09 '15 at 12:14
  • You have to make a subclass. And then change the class of the root view of the controller with button in storyboard. – Nikita Kukushkin Mar 09 '15 at 13:10
  • Thanks, I wrote an answer with the code I choose to use, thanks for your help! – Dor Cohen Mar 09 '15 at 15:29
  • When I present it following this advice, I'm unable to see the view behind it. My presented view controller background is opaque or pure black even though I set the background color to clear. – ScottyBlades Jun 11 '19 at 02:44
  • For anyone who's reading this in the future: If you need the ViewController in the back to receive touch events, then think about adding the new one as a child view controller. – Bartosz Kunat Apr 29 '20 at 09:40
6

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)
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
1

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

Thellimist
  • 3,757
  • 5
  • 31
  • 49