2010-08-25 100 views
5

我曾經在一個獨立的可可測試應用程序下面的代碼:NSTextView的NSScrollView內不會滾動:(

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

我試圖建立涉及對象的整個層次(包括文本系統對象)在一個NSTextView + NSScrollView組合中,看看它是如何一起工作的,但是當我運行這個並開始在文本視圖中添加一堆行時,當文本變得比視圖更長時它不會滾動就好像NSScrollView和NSTextView不知道對方一樣,我錯過了什麼連接讓這裏所有的東西都能正確通信?

編輯:是的,這是漏水和醜陋的。 :)這是爲了試圖確定這裏發生了什麼,而不是生產代碼或我將再次使用的任何東西。諾言。

回答

4
+0

的確我沒有。這似乎是答案。 :) 謝謝! – Sean 2010-08-25 19:46:04

+0

那和-setHeightTracksTextView:需要是NO。 – Sean 2010-08-25 19:47:48

+0

我*知道*我忘了一個,但沒有時間進行詳盡的搜索。 :-) – 2010-08-25 20:23:15