簡體   English   中英

NSAlert中的NSTextView沒有滾動條

[英]NSTextView in NSAlert has no scrollbars

我正在嘗試將NSTextView添加到NSAlert中,以便用戶可以在其中鍵入內容。 但是,無論用戶鍵入多少,滾動條都不會出現。 到底是怎么回事?

這是我正在使用的代碼,以及出現的對話框的屏幕截圖:

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

缺少滾動條的屏幕截圖

您需要將NSTextView放置在NSScrollView中。

Apple在本文檔中描述了該過程。

那里的主要代碼是:

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

在xib內部的屬性檢查器中,只需啟用垂直和水平滾動條即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM