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

元气小坏坏 提交于 2019-11-30 02:49:58

Here's another way to accomplish this that's a bit more clear and succinct:

[viewToBeMadeForemost removeFromSuperview];
[self addSubview:viewToBeMadeForemost positioned:NSWindowAbove relativeTo:nil];

Per the documentation for this method, when you use relativeTo:nil the view is added above (or below, with NSWindowBelow) all of its siblings.

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.

using @Dalmazio's answer in a Swift 4 category that mimics the equivalent UIView method gives the following:

extension NSView {

    func bringSubviewToFront(_ view: NSView) {
            var theView = view
            self.sortSubviews({(viewA,viewB,rawPointer) in
                let view = rawPointer?.load(as: NSView.self)

                switch view {
                case viewA:
                    return ComparisonResult.orderedDescending
                case viewB:
                    return ComparisonResult.orderedAscending
                default:
                    return ComparisonResult.orderedSame
                }
            }, context: &theView)
    }

}

so, to bring subView to the front in containerView

containerView.bringSubviewToFront(subView)

unlike solutions which remove and re-add the view, this keeps constraints unchanged

I was able to get this to work without calling removeFromSuperView

// pop to top
[self addSubview:viewToBeMadeForemost positioned:NSWindowAbove relativeTo:nil];

Below given code should work fine..

    NSMutableArray *subvies = [NSMutableArray arrayWithArray:[self subviews]];//Get all subviews..

    [viewToBeMadeForemost retain]; //Retain the view to be made top view..

    [subvies removeObject:viewToBeMadeForemost];//remove it from array

    [subvies addObject:viewToBeMadeForemost];//add as last item

    [self setSubviews:subvies];//set the new array..
Alfredo Campos

You can achieve this by just adding the view again; it will not create another instance of it.

[self addSubview:viewToBeMadeForemost];

You can Log the number of subviews before and after this line of code is executed.

To move a view to the front, it must be placed as the last element in the subviews array. Here's a Swift 3.0 solution:

func bringChildToFrontWith(viewIdentifier: Int) {
    var subViewArray = self.subviews // Apple docs recommend copying subViews array before operating on it, as it "can be changed at any time"
    for ix in 0 ..< subViewArray.count {
        if let childView = subViewArray[ix] as? MyViewSubClass {
            if childView.myViewSubClassIdentifier == viewIdentifier {
                let topItem = subViewArray[ix];
                subViewArray.remove(at: ix)
                subViewArray.append(topItem)
                self.contentView.subviews = subViewArray
            }
        }
    }
}
Guy

You might try this:

[viewToBeMadeFirst.window makeKeyAndOrderFront:nil];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!