【问题标题】:Resize a webview embed inside scrollview调整嵌入在滚动视图中的 webview 的大小
【发布时间】:2013-08-25 14:51:15
【问题描述】:

我有一个包含歌曲的 HTML 字符串的 sqlite 数据库。

视图控制器
在这个视图控制器中,我有一个文本和一个按钮,用户在其中输入歌曲编号和一个按钮,用于发送搜索数据库的请求。

歌曲显示
在这个视图控制器中,我有一个 web 视图和一个标签,它们都嵌入在滚动视图中。我的网页视图禁用滚动。

歌词显示,并且滚动视图正确调整大小。然而,网络视图并没有被调整大小,只显示了一些歌词。我知道这一点,因为我有不同颜色的滚动视图和网络视图。

滚动视图的顶部 https://www.dropbox.com/s/8hgpi8nlwnca0v8/iOS%20Simulator%20Screen%20shot%20Aug%2022%2C%202013%2C%202.04.24%20PM.png

这里的问题示例,滚动视图的底部 https://www.dropbox.com/s/r8l8d82an613hec/iOS%20Simulator%20Screen%20shot%20Aug%2022%2C%202013%2C%201.57.14%20PM.png

这是我的代码:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];

    frame.size = fittingSize;

    [self.scroll setContentSize:fittingSize]; //scroll is the name of the scroll view

    aWebView.frame = frame;

    NSLog(@"size: %f" ,fittingSize.height);
}

如何让 web 视图和滚动视图都调整大小?

【问题讨论】:

  • ScrollViews 中的 WebViews 不能正常工作。这可能很快就会好转。现在这将是有问题的。
  • 我也有同样的问题(

标签: ios sqlite uiwebview uiscrollview


【解决方案1】:

我通过在 detailView 中的 viewWillAppear 中加载内容解决了这个问题 像这样

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configureView];//<-- here i am call loadHTMLString
}

- (void)configureView
{

    // disabling scroll for webview
    if ([self.detailWebView respondsToSelector:@selector(scrollView)]) {
        self.detailWebView.scrollView.scrollEnabled = NO; // available starting in iOS 5
    } else {
        for (id subview in self.detailWebView.subviews)
            if ([[subview class] isSubclassOfClass: [UIScrollView class]])
                ((UIScrollView *)subview).scrollEnabled = NO;
    }

    // setting content
    if (self.detailItem) {
        self.detailTitle.text = self.detailItem->title;
        //image scaling styles
        NSString *style=@"<style type=\"text/css\">\n"
        "img { \n"
        "   display: block;"
        "   float:none;"
        "   margin:0 auto;"
        "   max-width:100%;"
        "   height:auto;"
        "} \n"
        "</style>\n";

        [self.detailWebView loadHTMLString:[style stringByAppendingString:self.detailItem->content] baseURL:nil];
    }
}

【讨论】:

    【解决方案2】:

    问题在 OS X Mavericks 正式发布后自行解决。之前的问题可能是由于 beta 版本中的错误造成的。

    【讨论】:

      【解决方案3】:

      根据 Apple 的建议,不应将 webview 嵌入到滚动视图中

      重要

      你不应该在 UIScrollView 中嵌入 UIWebView 或 UITableView 对象 对象。如果这样做,可能会导致意外行为,因为触摸 两个对象的事件可能会混淆和错误处理。

      参考: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多