Mac App WebView背景

时间:2012-08-29 14:43:58

标签: objective-c macos background webview

如何在Mac App中将图像设置为WebView的背景?因为WebView具有“弹跳”效果,所以如果向下滚动而不是内容,则背景通常只是白色。是否可以像UIWebView一样设置背景图像?

编辑:另一种方法是完全停用webView的弹跳效果。

3 个答案:

答案 0 :(得分:1)

如果您以编程方式创建此webview,则创建一个图像,然后创建webview。确保它们的位置(x,y)和尺寸(高度,宽度)相同。

如果您通过IB放下图像(通常只是拖动图像),然后将webview置于其上。

答案 1 :(得分:1)

我发现这是一种阻止弹跳的方法:

id sub = self.web.subviews[0];
if ([sub isKindOfClass:[WebFrameView class]])
    [sub setAllowsScrolling:NO];

可能不需要课程检查,但我想确定。

答案 2 :(得分:1)

我们可以使用cocoa在WebView中添加背景图像。要做到这一点,我们需要为body标签添加css。

创建网络视图:

WebView *webView;

获取图片格式包路径:

NSString *path = [[NSBundle mainBundle] pathForResource:@"my-bg" ofType:@"jpg"];

为正文标记写Css。

NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:@"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:@" background-image: url('file://%@');",path]];
[htmlBuilder appendString:@"}"];

在webview上加载此图片

[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];

您的图片将在webview的背景中添加。