Sibling NSView z-ordering in Cocoa

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

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

"

  • In IB

    you can place views on top of each other, and they will always be composed the way you expect.
  • Buttons in Xcode on a menu Editor

    named Send to Back, Send Forward, etc.
  • NSView

    also has a named method - (void)addSubview:(NSView *)aView positioned:(NSWindowOrderingMode)place relativeTo:(NSView *)otherView;

    , which seems to imply that there is a clear ordering.

Hypothesis B : "By no means, the sibling z-order NSViews

is undefined at runtime. Sometimes it works, sometimes it doesn't. Don't believe it!"

  • Apple 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?

+3


source to share


2 answers


Yes, NSView sisters have allowed to overlap, Apple docs are out of date: Are layer-based NSView siblings allowed?



In addition, the z-order depends on the order of the subviews in the parent view array subviews

.

+3


source


I was solving the same problem and found that at least on Mavericks it only works with layer support. I haven't had a chance to test it on other versions of OS X yet, but on Mavericks you need to set wantsLayer

in YES

for whatever views you want to overlay to get it to work.



+1


source







All Articles