问题
I have an NSScrollView inside an NSWindow, but it seems to be disabled. It looks like it would work, but the scrollbars are unresponsive to the mouse or the scroll wheel.
When I put the exact same NSScrollView inside a window on a new XCode project, it works perfect. There is something about the way I am making the window that is preventing the scroll from working.
I've been able to simplify it to this example:
//Make a window
NSWindow* myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(300, 300, 300, 300)
styleMask:NSTitledWindowMask
backing:NSBackingStoreRetained
defer:NO];
//Make a scroll view
NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)];
[scrollview setHasVerticalScroller:YES];
[scrollview setAcceptsTouchEvents:YES];
[myWindow setContentView:scrollview];
//Add something big to the scroll view
NSButton* btn = [[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 600, 900)] autorelease];
[scrollview setDocumentView:btn];
//Show the window
[NSApp arrangeInFront:self];
[myWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
Any ideas?
回答1:
Your problem, based on some experimentation I just did, seems to be with specifying NSBackingStoreRetained
. The docs say:
You should not use this mode. It combines the limitations of
NSBackingStoreNonretained
with the memory use ofNSBackingStoreBuffered
.
They also say:
In Mac OS X 10.5 and later, requests for retained windows will result in the window system creating a buffered window, as that better matches actual use.
This does not seem to be accurate; switching the buffer:
argument to NSBackingStoreBuffered
made the window and scroll view behave as expected for me. (The docs also say not to use NSBackingStoreNonRetained
, and indeed, it seemed to have problems similar to NSBackingStoreRetained
.)
来源:https://stackoverflow.com/questions/6553883/nsscrollview-in-a-nswindow