具有动态高度的UIWebView在Objective-C中的UIScrollView中

时间:2016-07-19 10:00:25

标签: ios objective-c iphone uiscrollview uiwebview

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

enter image description here

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

我尝试做到这一点:

  1. UIWebView属性"缩放页面以适应"是的;
  2. UIWebView未被取消;
  3. UIWebView不允许用户交互。
  4. 在我的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保持与第一次加载相同的高度。

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

4 个答案:

答案 0 :(得分:4)

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

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

加载您的网页视图和网络视图委托

- (void)webViewDidFinishLoad:(UIWebView *)webView

获取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;

}
希望它可以帮助你。

答案 1 :(得分:0)

尝试在viewDidLayoutSubviews()

中设置AssetManager assets = this.Assets; using (StreamReader sr = new StreamReader (assets.Open ("AedJson.json"))) { string json = sr.ReadToEnd (); }

答案 2 :(得分:0)

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

并且您可以使用此webView.scrollView.contentSize.heightcontentSize方法中设置滚动视图的webViewDidFinishLoad

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)];
}

答案 3 :(得分:0)

要设置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
  }
}