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

前端 未结 7 1044
既然无缘
既然无缘 2020-12-28 12:47

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 h

相关标签:
7条回答
  • 2020-12-28 13:37

    Another way is to use NSView's sortSubviewsUsingFunction:context: method to re-order a collection of sibling views to your liking. For example, define your comparison function:

    static NSComparisonResult myCustomViewAboveSiblingViewsComparator( NSView * view1, NSView * view2, void * context )
    {    
        if ([view1 isKindOfClass:[MyCustomView class]])    
            return NSOrderedDescending;    
        else if ([view2 isKindOfClass:[MyCustomView class]])    
            return NSOrderedAscending;    
    
        return NSOrderedSame;
    }
    

    Then when you want to ensure your custom view remains above all sibling views, send this message to your custom view's superview:

    [[myCustomView superview] sortSubviewsUsingFunction:myCustomViewAboveSiblingViewsComparator context:NULL];
    

    Alternatively, you can move this code to the superview itself, and send the message sortSubviewsUsingFunction:context: to self instead.

    0 讨论(0)
提交回复
热议问题