NSScrollView滚动条的长度错误 [英] NSScrollView scroll bars are of the wrong length

查看:286
本文介绍了NSScrollView滚动条的长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cocoa窗口,其内容视图包含一个NSScrollView,反过来,它包含一个固定大小的NSView。

I have a Cocoa window, whose content view contains an NSScrollView that, in turns, contains a fixed-size NSView.

启动程序时,滚动条显示的内容太小,似乎内容大小比实际大得多:

Upon launching the program, the scroll bars displayed initially are too small, as if the content size was much larger than it actually is:

当我开始玩,例如垂直滚动条,并将其带回到原始位置在顶部,它会调整为其预期大小(对应于滚动视图和内容视图大小的比例):

When I start playing with, e.g., the vertical scroll bar, and bring it back to the original position at the top, it gets resized to its expected size (which corresponds to the ratio of scroll view and content view sizes):

(请注意,水平条的大小仍然不正确,如果我)

(Notice the horizontal bar, which still has incorrect size. If I then play with it, and bring it back to its leftmost position, it gets resized to the correct size.)

推荐答案

我也可以使用它,然后使用它,并把它带回到最左边的位置,它会调整到正确的大小。遇到同样的问题,我搜索无处不在,但似乎没有人遇到这个问题。幸运的是,我发现一个解决问题的黑客。

I also encountered the same problem, I have searched everywhere but it seems no one else experiences this problem. Fortunately I found a hack which solves the problem.

我注意到,当窗口被调整大小或最大化时,滚动条调整为预期大小(自动调整必须启用)。这是因为当窗口调整大小时,scrollview和滚动条的长度被重新计算并被正确计算。可能由于一些错误,滚动条长度在初始化时不能正确计算。反正要解决这个问题,在你的应用程序委托创建一个出口到你的窗口。覆盖applicationDidFinishLaunching方法,并在窗口出口中调用方法frame,它返回窗口的当前NSRect。使用返回值将一个添加到size.width和size.height。调用方法setFrame,显示设置为YES。这将调整窗口大小并强制重新计算滚动条的大小。

What I did notice was that when the window is resized or maximized the scrollbars resize to the expected size (autoresizing has to be enabled). This is because when the window resizes so does the scrollview and the length of the scroll bars gets recalculated and is calculated correctly. Possibly due to some bug the scroll bar lengths are not calculated correctly on initialization. Anyway to fix the problem, in your application delegate create an outlet to your window. Override the "applicationDidFinishLaunching" method and inside it call the method "frame" on the window outlet, which returns the current NSRect of the window. Using the returned value add one to the size.width and size.height. The call the method setFrame with display set to YES. This will resize the window and force the size of the scrollbars to be recalculated.

这里是applicationDidFinishLaunching下面的代码

Here is the code for applicationDidFinishLaunching Below

(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

     // Get the current rect
     NSRect windowRect = [_window frame];`

     // add one to the width and height to resize window
     windowRect.size.width += 1;
     windowRect.size.height += 1;

     // resize window with display:YES to redraw window subviews
     [_window setFrame:windowSize display:YES];

}

这篇关于NSScrollView滚动条的长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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