简体   繁体   English

NSAlert中的NSTextView没有滚动条

[英]NSTextView in NSAlert has no scrollbars

I'm trying to add an NSTextView into an NSAlert so that the user can type into it. 我正在尝试将NSTextView添加到NSAlert中,以便用户可以在其中键入内容。 However, the scrollbars never appear, no matter how much the user types. 但是,无论用户键入多少,滚动条都不会出现。 What is going on? 到底是怎么回事?

Here is the code I'm using, and a screenshot of the dialog that appears: 这是我正在使用的代码,以及出现的对话框的屏幕截图:

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

缺少滚动条的屏幕截图

You need to place the NSTextView inside a NSScrollView. 您需要将NSTextView放置在NSScrollView中。

Apple describes the process in this document . Apple在本文档中描述了该过程。

The main code from there is: 那里的主要代码是:

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