Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make NSView NOT clip subviews outside of its bounds

Is it possible to make an NSView not clip its subviews that are outside of the bounds? On iOS I would simply set clipsToBounds of my UIView no NO. But NSView doesn't have such a property. I tried experimenting with wantsLayer, masksToBounds, wantsDefaultClipping, but all of these seem to only change the clipping of the drawRect method, not the subviews.

like image 737
DrummerB Avatar asked Jul 22 '13 16:07

DrummerB


3 Answers

The behaviour around this seems to have changed. You just need to set the view's layer to not mask to bounds.

view.wantsLayer = true
view.layer?.masksToBounds = false
like image 66
Avario Avatar answered Nov 01 '22 09:11

Avario


After 5 hours of struggling I've just achieve it. Just change class of any NSView in .storyboard or .xib to NoClippingView and it will NOT clip any of it's subviews.

class NoClippingLayer: CALayer {
    override var masksToBounds: Bool {
        set {

        }
        get {
            return false
        }
    }
}
    
class NoClippingView: NSView {
    override var wantsDefaultClipping: Bool {
        return false
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        wantsLayer = true
        layer = NoClippingLayer()
    }
}

Why do I override masksToBounds in NoClippingLayer? Because some native AppKit classes change this property of all sublayers in runtime without any warnings. For example, NSCollectionView do this for views of it's cells.

like image 13
Padavan Avatar answered Nov 01 '22 08:11

Padavan


I was able to solve this by overriding wantsDefaultClipping of the subviews to return NO.

like image 7
DrummerB Avatar answered Nov 01 '22 09:11

DrummerB