Reorder NSView

I am working on a custom control that represents different pens. I want to be sure that the selected descriptor has the term Z-index, and the rest are processed.

Is there a way to change the viewing order? I found this function sortSubviewsUsingFunction:context:, but I cannot figure out if this is the right solution.

+5
source share
4 answers

It's pretty simple, you can use a function that compares 2 subheadings to reorder them. Here's a simple solution based on a view 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;
        }
    }
}
+5
source

Cocoa API OSX 10.10.
iOS, .

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

documentation; NSWindowBelow NSWindowAbove, , .

+4

, sortSubviewsUsingFunction:context: , , , , , - () . bringSubviewToFront: UIView. , - IBAction :

[self.view bringSubviewToFront: handle];

Assuming the descriptor is an object that / inherits from the UIView. If this is not what you are looking for, then by all means go to sortSubviewsUsingFunction:context:, in which case you will need to change the individual tag properties for each view and sort them accordingly.

-3
source

As I understand from your question, you can loop everything that you are viewing and add them to the array, then you have the opportunity to add them to your view using the index .. and you have the following functions.

[self.view insertSubview:view1 atIndex:2]
[self.view insertSubview:view1 aboveSubview:0]
[self.view insertSubview:view1 belowSubview:1]

then you can change it as you need.

-5
source

All Articles