查找UIWebView的垂直堆叠布局的最终​​高度

时间:2014-02-08 23:03:45

标签: ios dynamic uiwebview uiscrollview height

我有一个带有几个容器的UIView,每个容器包含4个垂直堆叠的物品。

  • UILabel - name1(25px固定,如果存在,0px,如果没有)
  • UIWebView - desc1(80px固定为开始,但动态调整内容)
  • UILabel - name2(25px固定,如果存在,0px,如果没有)
  • UIWebView - desc2(80px固定为开始,但动态调整内容)

UIWebViews包含一小部分onViewDidLoad本地加载的混合文本/ html。在我尝试找到webview的动态高度后,我正在调整每个元素的框架,假设找到的高度是准确的。我在webview加载后调整大小。

然而,我在准确确定它们的动态高度方面遇到了很多麻烦,因此发生了很多重叠。通常高度太小,webview在其固定高度内滚动。

// resizeElements: adjusts all the frame heights/origins
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self resizeElements];
}

// not really working at all
// have tried various other methods using html height, etc, etc.
-(float) getWebViewContentSize:(UIWebView*) myWebView{
    NSString *heightString = [myWebView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int heightInt = [heightString intValue];
    return heightInt;
}

// seems proportionate, but is about 20% too small for larger amounts of content usually
//    and increasingly too small the longer the text gets.
-(float) getWebViewContentSize2:(UIWebView*) myWebView{
    CGSize size = myWebView.scrollView.contentSize;
    int height = size.height;
    return height;
}

任何更好的方法来获得身高或建议放置这4个元素?

1 个答案:

答案 0 :(得分:3)

有同样的问题。除了调用调整大小函数([self resizeElements]是你的情况),仍然找不到任何解决方案,延迟半秒。 在webViewDidFinishLoad之后调用[myWebView stringByEvaluatingJavaScriptFromString:@“document.body.offsetHeight”]时: - 文档高度始终等于当前webView框架高度。 它很难看,但它正在工作

编辑:我花了半个多小时的时间,并且发现[webView stringByEvaluatingJavaScriptFromString:@"document.height;"]只有在你加载html字符串之前设置了webView的框架才能正常工作。同样重要的是,帧的大小必须大于零(例如CGRectMake(0, 0, 1, 1) 这不能正常工作

编辑2:我又花了一个小时,最后的解决方案是:

layoutSubviews方法如下所示:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGRect actualFrame = self.view.bounds;

    _titleLabel.frame = CGRectMake(0, 0, actualFrame.size.width, 80);

    [self resizeWebview:_primaryWebView];
    _primaryWebView.frame = CGRectMake(0, _titleLabel.frame.origin.y + _titleLabel.frame.size.height,
                                       actualFrame.size.width, _primaryWebView.frame.size.height);


    _subtitleLabel.frame = CGRectMake(0, _primaryWebView.frame.origin.y + _primaryWebView.frame.size.height,
                                      actualFrame.size.width, 80);

    [self resizeWebview:_secondaryWebView];
    _secondaryWebView.frame = CGRectMake(0, _subtitleLabel.frame.origin.y + _subtitleLabel.frame.size.height,
                                         actualFrame.size.width, _secondaryWebView.frame.size.height);

}
- (void)resizeWebview:(UIWebView *)webView
{ 
    CGRect frame = webView.frame;

    frame.size.height = 1;
    webView.frame = frame;

    frame.size.height = [webView stringByEvaluatingJavaScriptFromString:@"document.height;"].floatValue;

    webView.frame = frame;
}

你也可以从

获得帧高
CGSize sizeThatFits = [webView sizeThatFits:CGSizeZero];
frame.size = sizeThatFits;

同样适用于这两种方法。

并且不要忘记在加载或改变方向后调整webViews 的大小:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.view setNeedsLayout];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.view setNeedsLayout];
}

所以,多亏了你,我重构了我的代码,最后摆脱了那个丑陋的时间延迟:)