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.
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.
It is pretty simple, you can use a function that compare 2 subviews to reorder them. Here a simple solution based on view's tag:
[mainView sortSubviewsUsingFunction:(NSComparisonResult (*)(id, id, void*))compareViews context:nil];
...
NSComparisonResult compareViews(id firstView, id secondView, void *context) {
int firstTag = [firstView tag];
int secondTag = [secondView tag];
if (firstTag == secondTag) {
return NSOrderedSame;
} else {
if (firstTag < secondTag) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With