Search code examples
cocoanswindownsscrollview

NSScrollView scroll bars are of the wrong length


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:

enter image description here

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

enter image description here

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


Solution

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

    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.

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