Change NSView subviews order

MatterGoal picture MatterGoal · Jul 3, 2012 · Viewed 9.7k times · Source

I'm working at a custom control that presents different handles. I want to be sure that the selected handle has a Z-index grater then the others handles.

Is there a way to swap view order? I found this function sortSubviewsUsingFunction:context: but i can't understand whether this is the right solution.

Answer

Axel Guilmin picture Axel Guilmin · Aug 9, 2015

Cocoa introduced a new API in macOS 10.0.
It's similar to iOS, you can pass another view to be displayed below or above.

[self.view addSubview:myView positioned:NSWindowBelow relativeTo:myViewInBackground];

Checkout the documentation; in my experience NSWindowBelow and NSWindowAbove seemed reversed though.