0

On my storyboard I have 2 viewControllers, one with a background image and one with blur effect an a label

storyboard

I would like to present the second view on top of the first one. I'm using the following code:

 override func viewDidAppear(animated: Bool) {
    var vc = self.storyboard?.instantiateViewControllerWithIdentifier("second") as secondViewController
    self.view.addSubview(vc.view)

    super.viewDidAppear(animated)
}

I'm using addSubView and not presentViewController, because the latest cause my first view to disappear after the transition completed, and all i got was a black screen.

as long as I don't add constraints to the second view, my code seems to be at the right direction (see below):

enter image description here

However, the moment I'm adding constraints to the second view, it will not show at all.

Q:What is the correct way of displaying a second view controller as an overlay and give it the right constraints

EDIT: I've tried to add vc.view.frame = self.view.frame as suggested by @topher91. The view appears for an instants then disappears. Here is the debug view hierarchy: enter image description here

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

2 Answers2

1

You should be creating the overlay as a different view rather than as a new view controller. Then in the storyboard you can set the constraints relative to the parent view controller:

Custom views with Storyboard

Alternatively, you can continue as you have done but before you addSubview set the frame of the overlay to be the same as the parent.

override func viewDidAppear(animated: Bool) {
    var vc = self.storyboard?.instantiateViewControllerWithIdentifier("second") as secondViewController

    vc.view.frame = self.view.frame
    self.view.addSubview(vc.view)

    super.viewDidAppear(animated)
}
Community
  • 1
  • 1
topher91
  • 222
  • 1
  • 5
  • Thanks for the reply, I will check it out. Do you know if that is the right approach of creating modal overlays that encapsulate some logic? – Shlomi Schwartz Dec 29 '14 at 19:58
0

At the end it was all a constraints issue. My code for adding the view was really simple:

var registrationVC = self.storyboard?.instantiateViewControllerWithIdentifier("registration") as RegistrationViewController
    self.view.addSubview(registrationVC.view)

and here are the constraints for the blur view:

<constraints>
    <constraint firstItem="iT5-uU-X9N" firstAttribute="leading" secondItem="V3l-eN-oQT" secondAttribute="leading" id="7EH-Cu-ZOY"/>
    <constraint firstAttribute="centerY" secondItem="iT5-uU-X9N" secondAttribute="centerY" id="AZj-sB-ByG"/>
    <constraint firstItem="fEQ-lT-dLv" firstAttribute="top" secondItem="iT5-uU-X9N" secondAttribute="bottom" id="KF2-8m-P2T"/>
    <constraint firstItem="iT5-uU-X9N" firstAttribute="top" secondItem="V3l-eN-oQT" secondAttribute="top" id="PsG-ox-lQL"/>
    <constraint firstAttribute="trailing" secondItem="iT5-uU-X9N" secondAttribute="trailing" id="cYs-qE-sVQ"/>
    <constraint firstAttribute="centerX" secondItem="iT5-uU-X9N" secondAttribute="centerX" id="qEL-Ln-Is9"/>
 </constraints>
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186