Joe Blow Joe Blow - 5 months ago 14
Swift Question

Incredibly, storyboard view order is not respected for container view views, fix?

Glance here ... http://stackoverflow.com/a/34736594/294884

enter image description here

in iOS the order in which you put sibling views, in the Inspector in Storyboard, of course becomes their z-order.

I was amazed to learn that this does not seem to apply to any such views that are container views.

To test, just make a scene with a few sibling views; a couple of them are container views. Run. Change something innocent in the class of the container view controllers (say, add a ViewDidLoad, constraint, or a print statement) and try again. The container view views are randomly moved to the top.

I find the only work around is you have to manually put them in order, in viewWillAppear, which is nuts.

1) could it be I've just screwed something up - maybe there's a setting ("respect storyboard order even for container views")

2) I've been scratching my head trying to figure out how to "automate" the workaround fix. You'd need to read the order of all views from the storyboard as it's loaded? And then "fix" it at viewWillAppear time. I'm not sure though.

3) could there perhaps be some trick .. in prepareForSegue say? .. to force them to be in the as-seen-in-storyboard sibling order?!




A fix...



One ridiculous fix for this just occurred to me.

Say you have a stretch of your storyboard with 10 sibling views, and say 4 of those are container views. (So, it actually won't work, as described here.) In fact, make it that even those four views are just (pointless) ordinary UIView, which then, hold the view-that-is-a-container-view. (Just have each container view expand to fill it's "holder" view - size the "holder" view as you wish in autolayout.) I guess that would work - man, what a suck!

Answer

That's odd.

What if you put each container view inside an ordinary UIView, with all of the container view's edges pinned to the ordinary UIView? I bet you 5 bucks that fixes it.