【问题标题】:WebView height not set based on its contentWebView 高度未根据其内容设置
【发布时间】:2015-03-04 22:44:00
【问题描述】:

我知道这个问题被发布了很多次,但我仍然无法根据其内容更改UIWebView 的高度

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"webview";
    [_webView setDelegate:self];

    NSString * string = @"<h1>This is HTML string</h1>";
    [_webView loadHTMLString:string baseURL:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"start loadinng.......");

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"finish loading.......");



    CGRect frame = _webView.frame;
    frame.size.height = 1;
    _webView.frame = frame;
    CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    _webView.frame = frame;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Failed to load with error :%@",[error debugDescription]);

}

谁能帮我调整UIWebView的高度?

【问题讨论】:

  • 为什么需要增加WebView的高度,如果超过它的高度,它可以滚动内容...
  • 你没有做任何改变webview高度的操作。
  • @KananVora :感谢您重播....因为我有 tableview 并且每一行都包含 webview 并且我的服务的响应是在 html 字符串中,因此需要根据其设置单元格和 webview 高度内容...请帮我解决非常紧急的问题
  • @iphonic:有什么解决办法吗??如何在 tableview 的行中设置 webview 高度??
  • 您可以在webViewDidFinishLoad委托方法中使用webview.scrollView.contentSize.height;获取webview内容的高度..

标签: ios objective-c iphone


【解决方案1】:

这是你的答案。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (webView.isLoading){
        return;
    }
    CGSize contentSize = webView.scrollView.contentSize;
    NSLog (@"height: %f",contentSize.height);
 }

在得到这个高度后,用新的宽度和高度重新构建你的 webView,如果有的话,也重新构建它的父视图。

【讨论】:

  • 我试过了,但它返回的内容大小是 504.00,而只有一个行字符串..所以根据我的观点,它将在 50 到 60 之间。你能告诉我我该如何处理吗? ?
  • 首先删除 [webView sizeToFit] 语句。还要检查你给 webView 的大小。你应该给它最小尺寸,然后用一行代码检查。然后再添加一些文本,然后检查它是否显示在所需的高度。
  • 我在我的 viewcontroll 中从 storyboard 设置 webview,默认大小为 0,0,320,150。
  • 尝试在 html 中添加更多数据。
【解决方案2】:

这个怎么样。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    [webView sizeToFit];
    webView.frame = CGRectMake(x,y,width,webView.frame.size.height);
}

【讨论】:

  • @BandishDave : 你有 webview 的代表吗? webViewDidFinishLoad 是否被调用?
  • :是的,先生,它被称为。
【解决方案3】:

我通常使用这些方法,将 UIWebview 框架设置为内容大小:

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

    frame.size.height = 5.0f;

    webView.frame = frame;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)];  // Pass about any size

    CGRect mWebViewFrame = webView.frame;


    mWebViewFrame.size.height = mWebViewTextSize.height;

    webView.frame = mWebViewFrame;


    //Disable bouncing in webview
    for (id subview in webView.subviews)
    {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        {
            [subview setBounces:NO];
        }
    }
}

当 WebView 完成加载其内容时,它们会被自动调用(如果您将 webviews 委托设置为此类)。


  1. check this

  2. raywenderlich-tutorial

这可能对你有帮助:)

【讨论】:

    【解决方案4】:
    First calculate height of web view for desired content.
    NSString* lString = @"<h1>This is HTML string</h1>";
    NSAttributedString* lAttributedText = [[NSAttributedString alloc] initWithData: [lString dataUsingEncoding:NSUTF8StringEncoding]
    options:@{ 
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
    NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
    }                                                               
    documentAttributes: nil 
    error:nil];
    
    CGRect lBoundingRect = [lAttributedText boundingRectWithSize: CGSizeMake(_webView.bounds.size.width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context: nil];
    
    then set it to _webView.frame
    
    Now, load web view
    [_webView setDelegate:self];
    
    [_webView loadHTMLString: lString baseURL:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2013-11-22
      • 1970-01-01
      • 2011-02-08
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多