开发者

NSTextView fails when used as contentView

开发者 https://www.devze.com 2023-02-10 07:53 出处:网络
I am programmatically creating a NSTextView to be the contentView for a window. When I create a WebView using the following code, it displays scrollbars, and the resizing thumb, and resizing the windo

I am programmatically creating a NSTextView to be the contentView for a window. When I create a WebView using the following code, it displays scrollbars, and the resizing thumb, and resizing the window works flawlessly.

When I comment out the WebView and attempt to use a NSTextView as the contentView, it does not "work" when the window is resized: Resizing the window using the thum开发者_开发知识库b causes the content of the text view to not repaint correctly, it also paints over the title of the window, and the resizing thumb is also not repainted.

-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
  NSTextView* view = [[NSTextView alloc] initWithFrame:[window frame]];
  // WebView* view = [[WebView alloc] initWithFrame:[window frame]];

  [window setContentView:view];
  [window makeFirstResponder:view];
  [window makeKeyAndOrderFront:view];  
}

Edit: Working code. Creates a NSScrollView to be the windows new contentView, and adds an NSTextView as its document.

-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
  NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[window frame]];
  NSTextView* view = [[NSTextView alloc] initWithFrame:[scrollView bounds]];
  [window setContentView:scrollView];
  [scrollView setDocumentView:view];
  [scrollView setHasVerticalScroller:YES];
  [scrollView setHasHorizontalScroller:YES];

  [window makeFirstResponder:view];
  [window makeKeyAndOrderFront:view];  
}


A web view makes and manages its own scrollers and is a special case rather than the norm. An NSTextView does not. It's just the text view. This is the norm - scrollable views come pre-wrapped in an NSScrollView only in the convenience of Interface Builder. In code, you must create an NSScrollView as well, then wrap the view in it. It's the scroll view that would be your top-level view in that case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号