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

前端 未结 4 1643
北恋
北恋 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

    The answer above by uchuugaka works very well and, as pointed out, it is very much simpler than older solutions.

    The code above calls the function centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(), but the source for this isn't provided.

    The function is trivial, but for completeness here's an an implementation:

    CGFloat centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension( CGFloat clipDimension, CGFloat docDimension)
    {
        CGFloat newOrigin;
        newOrigin = roundf((docDimension - clipDimension) / 2.0);
        return newOrigin;
    }
    

提交回复
热议问题