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

前端 未结 7 1042
既然无缘
既然无缘 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:17

    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

提交回复
热议问题