How do I make an NSView move to the front of all NSViews

Dhanaraj picture Dhanaraj · May 20, 2010 · Viewed 24.5k times · Source

I have a super view, which has 2 subviews. These subviews are overlapped.

Whenever i choose a view from a menu, corresponding view should become the front view and handle actions. i.e., it should be the front most subview.

acceptsFirstResponder resigns all work fine. But the mouse down events are sent to the topmost sub view which was set.

Regards, Dhana

Answer

Adam Preble picture Adam Preble · May 22, 2010

Here's another way to accomplish this that's a bit more clear and succinct:

[viewToBeMadeForemost removeFromSuperview];
[self addSubview:viewToBeMadeForemost positioned:NSWindowAbove relativeTo:nil];

Per the documentation for this method, when you use relativeTo:nil the view is added above (or below, with NSWindowBelow) all of its siblings.