5

Glance here ... https://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 workaround is: 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. Tricky.

3) could there perhaps be some trick .. in prepareForSegue say? .. to force them to be in correct Z-order, like any view!??


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 are holders for the container views. Suck!

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • This might be a special case, but often when I'm having Z issues manually re-ordering the offending View in the storyboard and then putting it back where you want it often fixes the problem. – GetSwifty Oct 04 '17 at 21:34
  • right, it's actually a special case - if it's a container view, the iOS system is buggy (I haven't checked with the latest iOS/Xcode) – Fattie Oct 05 '17 at 01:11

1 Answers1

2

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • hi Duncan - exactly, GMTA! :) notice my "A Fix..." at the end there. Yeah it's totally odd. Thinking it through, surely it is something to do with when the segues happen. (I've notice by slightly changing code, etc, in the sundry container view controllers, it can change the "random" order.) Weird right? Apart from anything else it's surprising hordes of angry programmers haven't noticed this (makes me think it might be my fault somehow??) – Fattie Jun 08 '16 at 19:35
  • I guess you only notice a change in z order when the views overlap and it either affects the way they overlap visually or the user needs to click and one view is blocking clicks to another. I don't think I've used a container view that way before, so I got lucky. – Duncan C Jun 08 '16 at 20:39
  • Hi @duncanC. thanks again for looking at this back then. I never really found a decisive explanation of the whole thing - some problems you just don't have time to get in to! Just as you say, the fix you suggest definitely fixes it (assuming it's a real problem and not some artifact of what I was doing then). Cheers! – Fattie Jun 30 '16 at 16:28