Search code examples
objective-ccocoanstableviewforegroundnsimageview

How to bring an NSImageView to the front or back of another NSImageView?


I have a NSTableView that holds a name to all of my NSImageView's, and depending on the order that the NSImageViews were added, the last one would be in the front.

But in the case that I want the user to be able to bring a NSImageViews in front of another, how would I do that?

Thanks in advance.


Solution

  • What you are asking about, I think, is how to control the z-order of views.

    For historical performance reasons this is not well supported in AppKit (unlike UIKit in iOS), since until somewhat recently you couldn't actually have sibling views that overlap.

    A common approach to this (on recent OS X releases) is to use Core Animation (in particular, CALayer) which does support z-ordering natively, but this is probably overkill for what you need (and in any event is going to have a learning curve for you).

    What are you actually trying to do? Are these images (image views) precisely on top of one another? If so, the easiest (and much better performing) approach is to have a single NSImageView and to just send -setImage:... to it to change the displayed image.