【问题标题】:How do I add a subView to a webView in OS X so It will scroll如何在 OS X 中将 subView 添加到 webView 以使其滚动
【发布时间】:2015-12-07 04:11:36
【问题描述】:

这应该有效,但无效。不过,类似的东西也可以在 iOS 中使用。

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSView<WebDocumentView>* doc = [[[self.webView mainFrame] frameView] documentView];
    TestView* testView = [[TestView alloc]init];
    [testView setFrame:CGRectMake(200, 400, 100, 50)];
    NSScrollView* sv = [doc enclosingScrollView];
    [sv addSubview:testView];
    [self.webView setNeedsDisplay:YES];
}

testView 出现但不会随着文档滚动。将其添加到发件人会产生相同的结果。将 testView 添加到 doc 不会产生可见的效果。

如果没有人知道另一种方法,我应该可以通过实时滚动通知来做到这一点。

谢谢

我已经按照你的建议做了。演示应用位于:https://dl.dropboxusercontent.com/u/108679476/webDemo.zip

我已经为 webView、doc 和 sv 尝试了 setNeedsDisplay。 AddToScrollView 按钮有效,但不会滚动或放大。 AddToDoc 按钮在我尝试过的任何条件下都不起作用。

scrollView 是否有可能因为不知道 doc 已更改而忽略 setNeedsDisplay?

谢谢

【问题讨论】:

  • 您不应该将子视图添加到滚动视图。如果你想在滚动视图中显示一些东西,你应该设置一个 documentView。 developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…
  • 我想通过在其上放置一个透明视图来突出显示现有文档的一部分。你是说我可以创建一个小的 documentView 并以某种方式将其设置在现有文档之上来完成此操作吗?我可以为此目的粘贴受支持的 UTI 粘贴板项目吗?
  • 如果您将子视图添加到滚动视图,那么您会破坏它。将其添加到文档视图。我宁愿在 scrollDidScroll 上更改 documentView 中的子视图位置。制作一个示例演示应用程序。用您尝试过的方法扩展您的问题。
  • 谢谢。我会按照你的建议去做。我需要几天的时间来处理任务。
  • 添加了演示。见上文。

标签: objective-c macos webview addsubview nsscrollview


【解决方案1】:

它没有移动,因为您将它添加到 NSClipView。如评论中所述,应将其添加到 documentView 中。完成后,我遇到了无法看到视图的问题。所以我启动了名为“Interface Inspector”的工具并验证了视图。它在那里,正确移动但没有被绘制。因此,我将您的视图更改为支持图层并开始工作。

-(IBAction)addToScrollView:(id)sender
{
  NSView<WebDocumentView>* doc = [[[self.webView mainFrame] frameView] documentView];
  TestView* testView = [[TestView alloc]init];
  [testView setFrame:CGRectMake(200, self.currentSVYCoordinate, 100, 50)];
  testView.wantsLayer = YES;
  CALayer *layer = [[CALayer alloc] init];
  layer.backgroundColor = [[NSColor redColor] CGColor];
  testView.layer = layer;
  self.currentSVYCoordinate = self.currentSVYCoordinate + 200;
  NSScrollView* sv = [doc enclosingScrollView];
  NSView *documentView  = [sv documentView];
  [documentView addSubview:testView];
  [sv setNeedsDisplay:YES];
}

【讨论】:

  • 你太棒了,xhruso00。谢谢
【解决方案2】:

这似乎是正确的方法。它会滚动并响应 mouseDown,但不会放大:

-(IBAction)addToScrollView:(id)sender
{
NSView<WebDocumentView>* doc = [[[self.webView mainFrame] frameView] documentView];
TestView* testView = [[TestView alloc]init];
[testView setFrame:CGRectMake(200, self.currentSVYCoordinate, 100, 50)];
self.currentSVYCoordinate = self.currentSVYCoordinate + 200;

//Use testview's drawRect to add a color

NSScrollView* sv = [doc enclosingScrollView];
[sv.contentView addSubview:testView];
[self.webView setNeedsDisplay:YES];

}

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    相关资源
    最近更新 更多