如何让NSScrollView在10.9和更高版本中居中文档视图? [英] How do you get NSScrollView to center the document view in 10.9 and later?

查看:266
本文介绍了如何让NSScrollView在10.9和更高版本中居中文档视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多例子,如何获取NSScrollView的中心其文档视图。
下面是两个例子
(这是相似的,有人是复制某人没有归属,但点的是如何有。)
http://www.bergdesign.com/developer/index_files/88a764e343ce7190c4372d1425b3b6a3-0.html
https://github.com/devosoft/avida/blob/master/apps/viewer-macos/src /main/CenteringClipView.h

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 without attribution, but the point of how is there.) http://www.bergdesign.com/developer/index_files/88a764e343ce7190c4372d1425b3b6a3-0.html https://github.com/devosoft/avida/blob/master/apps/viewer-macos/src/main/CenteringClipView.h

这通常是通过子类化NSClipView并覆盖:

This is normally done by subclassing NSClipView and overriding:

- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

但是此方法在Mac OS X 10.9 +

But this method is deprecated in Mac OS X 10.9 +

我们现在可以做什么?
哦no no〜〜

What can we do now? Oh noes~!

推荐答案

好吧,答案很简单,
这些不工作与双击放大反正。

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.

@implementation 中,您只需要实施 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;
}

在@interface中只需添加一个属性。这允许您不使用居中。

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;

此外,请务必将此 BOOL 设置为 YES NO 在您覆写

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

initWithFrame initWithCoder:

,因此您将有一个已知的默认值。

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

记住孩子, initWithCoder:允许你做一个需要并在一个笔尖)

(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!)

当然,如果您需要支持早于10.9的任何内容,需要实现其他东西。

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...)

这篇关于如何让NSScrollView在10.9和更高版本中居中文档视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆