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

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
    }
}

Well, the answer is simple and nowhere near as over bloated as those are. Those do not work with double-tap magnification anyway.

This does. It just works. You can also customize your adjustments as needed.

In the @implementation you only need to implement an override of constrainBoundsRect:

- (NSRect)constrainBoundsRect:(NSRect)proposedClipViewBoundsRect {

    NSRect constrainedClipViewBoundsRect = [super constrainBoundsRect:proposedClipViewBoundsRect];

    // Early out if you want to use the default NSClipView behavior.
    if (self.centersDocumentView == NO) {
        return constrainedClipViewBoundsRect;
    }
    
    NSRect documentViewFrameRect = [self.documentView frame];
                
    // If proposed clip view bounds width is greater than document view frame width, center it horizontally.
    if (proposedClipViewBoundsRect.size.width >= documentViewFrameRect.size.width) {
        // Adjust the proposed origin.x
        constrainedClipViewBoundsRect.origin.x = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.width, documentViewFrameRect.size.width);
    }

    // If proposed clip view bounds is hight is greater than document view frame height, center it vertically.
    if (proposedClipViewBoundsRect.size.height >= documentViewFrameRect.size.height) {
        
        // Adjust the proposed origin.y
        constrainedClipViewBoundsRect.origin.y = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.height, documentViewFrameRect.size.height);
    }

    return constrainedClipViewBoundsRect;
}


CGFloat centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension
(CGFloat proposedContentViewBoundsDimension,
 CGFloat documentViewFrameDimension )
{
    CGFloat result = floor( (proposedContentViewBoundsDimension - documentViewFrameDimension) / -2.0F );
    return result;
}

In the @interface just add one single property. This allows you to not use centering. As you can imagine, there may be conditional logic you want to turn centering off at times.

@property BOOL centersDocumentView;

Also, be sure to set this BOOL to YES or NO in your override of

initWithFrame and initWithCoder:

so you'll have a known default value to work from.

(remember kids, initWithCoder: allows you to do the needful and set a view's class in a nib. Don't forget to call super prior to your stuff!)

Of course if you need to support anything earlier than 10.9 you'll need to implement the other stuff.

(though probably not nearly as much as others have...)

EDIT: As noted by others, Apple has sample code in Swift (albeit from 2016 so it might not be the current Swift :D) at https://developer.apple.com/library/archive/samplecode/Exhibition/Listings/Exhibition_CenteringClipView_swift.html