【问题标题】:UIWebView with dynamic height inside a UIScrollView in Objective-C在 Objective-C 中的 UIScrollView 内具有动态高度的 UIWebView
【发布时间】:2016-11-22 03:45:46
【问题描述】:

我正在尝试制作具有“动态屏幕高度”的 UIWebView。此 webview 位于 UIScrollView 内,位于其他组件下方。查看布局(它使用自动布局):

我的想法是只提供一个滚动条(来自 UIScrollView - 它的工作)并使 WebView 视口根据内容大小而增长。而且它不起作用。

我做了什么来尝试这样做:

  1. UIWebView 属性“缩放页面以适应”已开启;
  2. UIWebView 未分页;
  3. UIWebView 不允许用户交互。

在我的 UIViewController 中:

- (void) viewDidAppear:(BOOL)animated {
    NSString *urlString = @"MY URL GOES HERE";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [_webViewDescription loadRequest:urlRequest];
    _webViewDescription.delegate = self;
    _webViewDescription.scrollView.scrollEnabled = NO;
    _webViewDescription.scrollView.bounces = NO;
}

我的 UIWebViewDelegate:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;

    webView.frame = frame;


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

    _myScrollView.contentSize = webView.bounds.size;
}

当我运行代码时,它会打印正确的 UIScrollView 大小和 UIWebView 大小。 UIScrollView 的高度变大了,但 UIWebView 保持与第一次加载相同的高度。

我正在真实设备中进行测试。

【问题讨论】:

  • 我设置的动态高度。但是我没有为布局约束设置它。请问你。我的代码有用吗?

标签: ios objective-c iphone uiscrollview uiwebview


【解决方案1】:

为您的 webView 添加一个高度约束并制作一个类似的 IBOutlet

@property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;

加载您的 webview 并在 web view 中委托

- (void)webViewDidFinishLoad:(UIWebView *)webView

获取网页视图内容高度并设置webViewHeightConstraint.constant 如下:-

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

    NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
    CGFloat height = str.floatValue;
    webViewHeightConstraint.constant = height;

}

希望对你有帮助。

【讨论】:

  • 你懂的。
  • @user3182143 编码快乐!干杯
【解决方案2】:

尝试在viewDidLayoutSubviews()中设置webView.frame = frame;

【讨论】:

    【解决方案3】:

    您可以使用webView.scrollView.contentSize.height获取webview内容的内容大小。

    然后你可以使用这个webView.scrollView.contentSize.heightwebViewDidFinishLoad 方法中设置滚动视图的contentSize

    webViewDidFinishLoad方法中像这样

    [objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
    if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
        [objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
    }
    

    【讨论】:

    • 它没有改变 uiwebview 的内容高度。
    【解决方案4】:

    设置UIWebView的动态高度,需要在webViewDidFinishLoadView方法中设置高度

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
      CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
       //For Width
     //CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
     CGRect frame = webview1.frame;
     frame.size.height = height;
     //frame.size.width = width; //Width
     webview.frame = frame;
    
     CGRect screenBounds = [UIScreen mainScreen].bounds ;
     CGFloat widthForIpad = CGRectGetWidth(screenBounds)  ;
     CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
     NSLog(@"The iPad width is - %fl",widthForIpad);
     NSLog(@"The iPad height is - %fl",heightForIpad);
    
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
     {
       if ([[UIScreen mainScreen] bounds].size.height == 568)
         scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
       else
         scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
      }
      else
      {
        if ([[UIScreen mainScreen] bounds].size.height == 1024)  
            scrollView.contentSize = CGSizeMake(320, height+370);  //For iPad
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 2015-04-24
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 2017-10-26
      相关资源
      最近更新 更多