NSScrollView内的NSTextView不会滚动:(

Sea*_*ean 5 cocoa objective-c appkit

我在一个独立的Cocoa测试应用程序中有以下代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSView *contentView = [window contentView];

    NSTextStorage *textStorage = [NSTextStorage new];
    NSLayoutManager *layoutManager = [NSLayoutManager new];
    NSTextContainer *textContainer = [NSTextContainer new];

    [textContainer setHeightTracksTextView:YES];
    [textContainer setWidthTracksTextView:YES];
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];

    NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:[contentView bounds]];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    [scrollView setBorderType:NSNoBorder];

    NSRect textFrame;
    textFrame.origin = NSZeroPoint;
    textFrame.size = [NSScrollView contentSizeForFrameSize:[scrollView frame].size hasHorizontalScroller:NO hasVerticalScroller:YES borderType:NSNoBorder];

    NSTextView *textView = [[[NSTextView alloc] initWithFrame:textFrame textContainer:textContainer] autorelease];
    [textView setAutoresizingMask:NSViewWidthSizable];

    [scrollView setDocumentView:textView];

    [contentView addSubview:scrollView];
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在NSTextView + NSScrollView组合中设置所涉及对象的整个层次结构(包括文本系统对象),以便了解它们如何一起工作.但是,当我运行它并开始向文本视图添加一堆行时,当文本变得比视图高时,它不会滚动.就像NSScrollView和NSTextView彼此不了解一样.我错过了什么联系,让一切都在这里正确沟通?

编辑:是的,这是漏洞和丑陋的.:)这只是为了试图确定这里发生了什么,而不是生产代码或我将直接再次使用的任何东西.诺言.