3

(asking and self-answering, since I found no hits on Google, but managed to find a solution in the end by trial and error)

With iOS 5 and 6, Apple added some ugly hacks to make InterfaceBuilder support "embedded" viewcontrollers. They didn't document how those work, they only give code-level examples, and they only cover a limited subset of cases.

In particular, I want to have an embedded viewcontroller that is sometimes hidden - but if you try the obvious approach it doesn't work (you get a white rectangle left behind):

childViewController.view.hidden = TRUE;
Adam
  • 32,900
  • 16
  • 126
  • 153

4 Answers4

5

Why don't you just create an IBOutlet to your container view and do

self.containerView.hidden = YES;
JonahGabriel
  • 3,066
  • 2
  • 18
  • 28
3

How they've done it appears to be a variation on the manual way that worked since iOS 2 (but which only supported views, not viewcontrollers) - there is a real, genuine UIView embedded into the parent (not mentioned in the source code examples - it's only added when you use InterfaceBuilder!).

So, instead, if you do:

childViewController.view.superview.hidden = TRUE;

...it works!

Also, counterintuitively, you can call this method at any time from viewDidLoad onwards - the "embed segue" hack from Apple is executed before viewDidLoad is called.

So you can do this on startup to have your childViewController start off invisible.

Adam
  • 32,900
  • 16
  • 126
  • 153
  • None of this is a big revelation. If you add a container view and look at its class, you will see that it's just a UIView, and that like any other UIView, it has a default white background color. – rdelmar Aug 26 '13 at 22:13
  • Apple's docs describe this differently. They don't mention the "extra" UIView. They also don't document the "embed segue" at all (it only appears in WWDC videos, so far as I can tell from googling). Anything undocumented suprises me :) – Adam Aug 26 '13 at 22:25
  • Well, I would agree that the documentation on this is definitely sparse. – rdelmar Aug 26 '13 at 22:31
  • If you are adding the view controller though IB, you actually see the embed segue. – JonahGabriel Aug 26 '13 at 22:32
  • @Jonah.at.GoDaddy yep. you see the segue. But it does NOT work like a normal Segue (c.f. it fires before viewDidLoad!). Does it require a containerview? Is the containerview only required by Xcode/NIB? :( – Adam Aug 29 '13 at 09:48
  • You saved me a lot of work adding and removing my embedded viewcontrollers just to hide/unhide them. Thanks! – Lensflare Sep 22 '14 at 15:36
3

Use This [self.childviewController setHidden:YES];

iosLearner
  • 1,312
  • 1
  • 16
  • 30
0

In case somebody will need to hide/show all child views or iterate over them:

func hideChildrenViews() {
    for view in self.view.subviews {
        (view as! UIView).hidden = true
    }
}

func showChildViews() {
    for view in self.view.subviews {
        (view as! UIView).hidden = false
    }
}
Oleg Novosad
  • 2,261
  • 1
  • 27
  • 28