0

I create a view controller vc in storyboard and place a View1 on it and add all constraint to it. Now I want to add a vc as a subview. I use this code :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LeftMenuListIdentifier")      
self.view.addSubview(vc.view)

vc controller's add as subview to my current controller but it does not show any subview of vc controller'view means View1.

If I push vc controller to my current view controller then it shows View1.

self.navigationController?.pushViewController(vc, animated: true)

Please suggest what should I do . I want to add a view controller from storyboard as a subview.

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
user100
  • 327
  • 5
  • 19

2 Answers2

2

Kindly Follow below steps for resolve your issue.

  1. First Create UIViewController object.
  2. after that Add object as a childviewcontroller
    as Like ( self.addChildViewController("Your Controller Object"))
  3. After that add controller on self.view as add subview.( `self.view.addSubview("Your Controller Object"))

i think it will working

Pitambar Indoria
  • 380
  • 1
  • 12
1

you don't need to push your vc, just add it to current viewcontroller using addChildViewController(vc);, this link may help you

Community
  • 1
  • 1
Pratik Jamariya
  • 810
  • 1
  • 10
  • 35
  • Hi!Thanks for reply I working now . but I need to add it on main window How do i do this? like this let currentWindow : UIWindow = UIApplication.sharedApplication().keyWindow! currentWindow.addSubview(vc.view) – user100 Apr 14 '16 at 06:24
  • what do you mean? do you want to make your child viewcontroller as rootviewcontroller? or do you to add your childviewcontroller to navigation hierarchy? – Pratik Jamariya Apr 14 '16 at 07:11