How do you get NSScrollView to center the document view in 10.9 and later?

前端 未结 4 1623
北恋
北恋 2020-12-23 23:36

There are lots of examples out there of how to get NSScrollView to center its document view. Here are two examples (which are so similar that somebody is copying somebody

4条回答
  •  有刺的猬
    2020-12-23 23:55

    Here working class for swift

    class CenteredClipView:NSClipView
    {
        override func constrainBoundsRect(proposedBounds: NSRect) -> NSRect {
    
            var rect = super.constrainBoundsRect(proposedBounds)
            if let containerView = self.documentView as? NSView {
    
                if (rect.size.width > containerView.frame.size.width) {
                    rect.origin.x = (containerView.frame.width - rect.width) / 2
                }
    
                if(rect.size.height > containerView.frame.size.height) {
                    rect.origin.y = (containerView.frame.height - rect.height) / 2
                }
            }
    
            return rect
        }
    }
    

提交回复
热议问题