Rectangle 27 0

objective c Change NSView subviews order?


[self.view bringSubviewToFront: handle];

Ah, in that case, this looks a little hackish, but you could always add a category to do the following: stackoverflow.com/questions/4236304/

Assuming that handle is an object that is/inherits from UIView. If this isn't what you're looking for, then by all means, go with sortSubviewsUsingFunction:context:, in which case you'll need to alter the individual tag properties for each subview, and sort accordingly.

I'm working on OSX non iOS... but +1 because I can search for similar functions.

No problem! I can't say authoritatively that it would be faster, but my thought would be a solid yes. This hinges on the internals of sortSubviewUsingFunction:context:, which I can't comment on. I imagine, however, that it works something like this: 1) manipulate the internal array of subview pointers, then 2) repaint the UI. Certainly ours will be faster for step 1, given that it doesn't iterate through all the subviews like the sort would. For step 2, it's probably either faster or equal, in that the implementation will either repaint all (ours is faster) or repaint just the change (equal).

Thank you, I need to understand if this solution, in terms of performance, is better then the sortSubviewsUsingFunction

Yes, sortSubviewsUsingFunction:context: can do what you're looking to do, however it might be overkill if all you'd like is for one particular view to sit on top of the (unordered) others. In that case, you should look into the bringSubviewToFront: method of UIView. That way, you could do something like this in your IBAction for bringing up the handle:

Note
Rectangle 27 0

objective c Change NSView subviews order?


[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;
        }
    }
}

I'm sorry but I can't understand the prototype of the compareViews function. Does it use a mix of Objective-c and C?

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:

Note
Rectangle 27 0

objective c Change NSView subviews order?


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

As i understand from you question you can loop all you view and add them to array then you have the option to add them to you view with index .. and you have the following functions.

Looks like the OP was asking about NSViews this is only for UIViews

then you can swap it's order as you need..

Note