2014-09-22 33 views
0

我試圖將NSTextView添加到NSAlert中,以便用戶可以輸入它。但是,無論用戶鍵入多少,滾動條都不會出現。到底是怎麼回事?NSAlert中的NSTextView沒有滾動條

這裏是我使用的代碼,然後在出現的對話框的截圖:

NSAlert *alert = [NSAlert alertWithMessageText:@"Enter stuff here:" 
           defaultButton:@"OK" 
           alternateButton:nil 
            otherButton:nil 
        informativeTextWithFormat:@""]; 



NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 200, 50)]; 
[alert setAccessoryView:textView]; 
[alert runModal]; 

Screenshot of lack of scrollbars

+0

您需要將NSTextView放置在NSScrollView中 – idmean 2014-09-22 17:01:10

+0

糟糕,我認爲它自動包含在NSTextView中。如果你想創建你的答案作爲一個單獨的答案,我會接受它。謝謝! – user2406467 2014-09-22 17:12:12

回答

1

你需要把NSTextView一個NSScrollView內。

Apple在this document中描述了該過程。

的主要代碼從有:

NSScrollView *scrollview = [[NSScrollView alloc] 
      initWithFrame:[[theWindow contentView] frame]]; 
NSSize contentSize = [scrollview contentSize]; 

[scrollview setBorderType:NSNoBorder]; 
[scrollview setHasVerticalScroller:YES]; 
[scrollview setHasHorizontalScroller:NO]; 
[scrollview setAutoresizingMask:NSViewWidthSizable | 
      NSViewHeightSizable]; 
theTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 
      contentSize.width, contentSize.height)]; 
[theTextView setMinSize:NSMakeSize(0.0, contentSize.height)]; 
[theTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)]; 
[theTextView setVerticallyResizable:YES]; 
[theTextView setHorizontallyResizable:NO]; 
[theTextView setAutoresizingMask:NSViewWidthSizable]; 

[[theTextView textContainer] 
      setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)]; 
[[theTextView textContainer] setWidthTracksTextView:YES]; 
[scrollview setDocumentView:theTextView]; 
[theWindow setContentView:scrollview]; 
[theWindow makeKeyAndOrderFront:nil]; 
[theWindow makeFirstResponder:theTextView]; 
[[theTextView enclosingScrollView] setHasHorizontalScroller:YES]; 
[theTextView setHorizontallyResizable:YES]; 
[theTextView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; 
[[theTextView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; 
[[theTextView textContainer] setWidthTracksTextView:NO]; 
0

在內部屬性檢查器中的XIB只是使垂直和水平滾動條。

+0

謝謝,不過,我正在尋找一種以編程方式執行此操作的方法。應該在我的問題中具體說明。 – user2406467 2014-09-22 17:30:16