ios - 将 subview 添加到 UIWebView 的内部 ScrollView

标签 ios objective-c uiwebview

我的目标是将 Google DFP 横幅 View (横幅广告)添加到 HTML 页面并使其在 iPad 上随 HTML 滚动。

HTML 使用 UIWebView 显示,这在 iOS 上是正常的。

如何将 DFP 横幅 View (继承自 UIView)与内容放在一起,以便它像页面的一部分一样滚动?

到目前为止我做了什么:

  • UIWebView 有一个 *scrollview 属性。
  • 遍历 webview.scrollview 的 subview 。
  • 对于每个 View ,调整框架以将它们向下移动 通过横幅的高度,并使它们变小相同的量。
  • 将横幅 View 插入 WebView 的 scrollview

    for (UIView *view in self.webview.scrollView.subviews) {
        CGRect frame = view.frame;
        frame.origin.y += bannerAdvertsHeight;
        frame.size.height -= bannerAdvertsHeight;
        view.frame = frame;
    }
    
    [self.webview.scrollView addSubview:self.dfpBannerView];
    

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

最佳答案

这是 UIWebView 的 View 层次结构:

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

尽管这是一种黑客行为,因此您可能会在以后的 iOS 更新中遇到麻烦。

关于ios - 将 subview 添加到 UIWebView 的内部 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28138794/

相关文章:

ios - 降低 iPod 音量以播放应用声音(如原生 SMS 应用)

objective-c - Objective-C 是如何编译的?

objective-c - iOS:UIView 的简单物理

iphone - UIwebView链接延迟

ios - 如何在 UIWebView 加载其初始请求之前设置本地存储?

ios - 许多文件的 attributesOfItemAtPath

ios - 使用 CGAffine 旋转 View 后的平移

ios - iPad OS 13 beta UIWebView在开关输入上崩溃

ios - 如何将 uilabel 高度设置为 0。或将 uibleviewcell 高度设置为 0?

iOS 框架问题 : Class is implemented in both