Skip to content

Instantly share code, notes, and snippets.

@mmuszynski
Created July 3, 2018 13:20
Show Gist options
  • Save mmuszynski/c062df8711981d75c3369512b697a83a to your computer and use it in GitHub Desktop.
Save mmuszynski/c062df8711981d75c3369512b697a83a to your computer and use it in GitHub Desktop.
class CenteredClipView: NSClipView {
override func constrainBoundsRect(_ proposedBounds: NSRect) -> NSRect {
var rect = super.constrainBoundsRect(proposedBounds)
if let containerView = self.documentView {
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
}
}
class ImageWell: NSImageView {
override var image: NSImage? {
didSet {
self.frame.size = image?.size ?? self.frame.size
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment