7

How does z-ordering work with sibling NSViews in Cocoa? I'm confused because I'm finding conflicting sources of information in Apple's docs and APIs. (Note: Subviews are obviously rendered on top of its parent view, I am talking explicitly about sibling views here).

Hypothesis A: "Yes, you can define the z-order of sibling NSViews"

  • In IB you can place views on top of each other and they will always be composited in the way you'd expect.
  • There's buttons in Xcode under the Editor menu named "Send to Back", "Send Forward" etc.
  • The NSView also has a method named - (void)addSubview:(NSView *)aView positioned:(NSWindowOrderingMode)place relativeTo:(NSView *)otherView; which seems to imply that there is a well defined ordering.

Hypothesis B: "No way, z-order of sibling NSViews is undefined at runtime. Sometimes it works, sometimes it doesn't. Don't trust it!"

  • Apple's Docs (View Programming guide) state: For performance reasons, Cocoa does not enforce clipping among sibling views or guarantee correct invalidation and drawing behavior when sibling views overlap. If you want a view to be drawn in front of another view, you should make the front view a subview (or descendant) of the rear view.

So which one is it?

lmirosevic
  • 15,787
  • 13
  • 70
  • 116

2 Answers2

4

Yes, NSView siblings are allowed to overlap, the Apple docs are out of date: Are layer-backed NSView siblings allowed to overlap?

Also, the z-order depends on the order of the subviews in the parent view's subviews array.

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
  • What if views are not layer backed? – lmirosevic Feb 07 '13 at 16:00
  • No, Apple docs are not out of date. There are cases when displaying won't work correctly in case views are not layered. explanation in comments at the link above. – dev_null Nov 06 '14 at 12:54
  • @dev_null Could you specify which link and which comments you mean? Thanks! – Johannes Fahrenkrug Nov 06 '14 at 13:06
  • yours above: stackoverflow.com/a/10729201/171933 , my comments below. The thing I state is that there is such kind of NSView (I can't say precisely what technology is used inside chromium browser) that siblings which are higher in Z order will be ignored. I suspect that having any opengl view is enough for this. – dev_null Nov 06 '14 at 16:18
  • 1
    Your mention of the order of the subviews array led me to pull up my project.xib in a text editor and move my imageview near the top of the array just under the – Lor Mar 23 '18 at 22:48
3

I've been solving the same issue and found that at least on Mavericks it works only with layer backed views. I didn't have chance so far to test it on other OS X versions, but on Mavericks you have to set wantsLayer to YES on all views you want to overlap in order to make it work.

Matthes
  • 515
  • 5
  • 12