向 UIWebView 的内部滚动视图添加子视图

Adding subviews to UIWebView's internal scrollview

我的目标是将 Google DFP 横幅视图(横幅广告)添加到 HTML 的页面,并让它随着 HTML 在 iPad 上滚动.

HTML 在 iOS 上使用 UIWebView 正常显示。

如何将 DFP 横幅视图(继承自 UIView)与内容放在一起,使其像页面的一部分一样滚动?

到目前为止我做了什么:

我目前无法访问 iPad,但它可以在模拟器上运行。我不确定这是否是 'good' 解决方案。如果您认为我的方法有任何问题,请告诉我。

这是 UIWebView 的视图层次结构:

(gdb) po [webView recursiveDescription]
<UIWebView: 0x68220e0; frame = (0 0; 320 460); >
| <UIScrollView: 0x4b2bee0; frame = (0 0; 320 460); >
|    | <UIImageView: 0x4b2dca0; frame = (0 0; 54 54); >
|    | <UIImageView: 0x4b2da20; frame = (0 0; 54 54); >
|    | <UIImageView: 0x4b2d9c0; frame = (0 0; 54 54); >
|    | <UIImageView: 0x4b12030; frame = (0 0; 54 54) >
|    | <UIImageView: 0x4b11fd0; frame = (-14.5 14.5; 30 1); >
|    | <UIImageView: 0x4b11f70; frame = (-14.5 14.5; 30 1); >
|    | <UIImageView: 0x4b11f10; frame = (0 0; 1 30); >
|    | <UIImageView: 0x4b11eb0; frame = (0 0; 1 30); >
|    | <UIImageView: 0x4b11e50; frame = (0 430; 320 30); >
|    | <UIImageView: 0x4b2d0c0; frame = (0 0; 320 30);  >
|    | <UIWebBrowserView: 0x6005800; frame = (0 0; 320 460); >

查看层次结构,您可能应该将横幅添加到 UIWebBrowserView,如果没有,则在其顶部:

for (id view in self.webview.scrollView.subviews) {
    if (![view isKindOfClass:[UIImageView class]]) {
        CGRect frame = view.frame;
        frame.origin.y += bannerAdvertsHeight;
        frame.size.height -= bannerAdvertsHeight;
        view.frame = frame;
    }
} 
[self.webview.scrollView addSubview:self.dfpBannerView];

虽然这有点像 hack,所以您可能会在以后的 iOS 更新中遇到麻烦。