【问题标题】:How to find the Height of content of Scroll View or WebView in iPhone [duplicate]如何在 iPhone 中查找 Scroll View 或 WebView 内容的高度 [重复]
【发布时间】:2013-05-30 09:26:45
【问题描述】:
 UIScrollView *ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,600)];
 ScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth ;
 ScrollView.autoresizesSubviews = YES;

[ScrollView setContentSize:CGSizeMake(320, 2500)];

ScrollView.scrollEnabled = YES;

ScrollView.directionalLockEnabled = YES;

ScrollView.showsVerticalScrollIndicator = NO;

ScrollView.showsHorizontalScrollIndicator = NO;

ScrollView.autoresizesSubviews = YES;
ScrollView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:ScrollView];

  UIwebview *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10,30,310,1500)];
        webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ;
        webView.autoresizesSubviews = YES;

        NSLog(@"vlaue od v is %@",webView);

        webView.scrollView.scrollEnabled = NO;
        webView.scrollView.bounces = NO;

        //make the background transparent
        [webView setBackgroundColor:[UIColor clearColor]];

        //pass the string to the webview

        [webView loadHTMLString:[html description] baseURL:nil];
        NSLog(@"web view is %f",webView.scrollView.contentSize.width);
        [webView sizeThatFits:CGSizeZero];
        NSLog(@"web view is %@",webView);

        //add it to the subview
        NSLog(@"height: %d", [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] intValue]);

    // CGFloat contentHeight = webView.scrollView.contentSize.height;
    //  ScrollView.contentSize = webView.scrollView.contentSize.frame.size;
   //     NSLog(@"scroll size is %d",ScrollView.contentSize);

     //   [UIScrollView setNeedsDisplay];

        [ScrollView addSubview:webView];

      NSLog(@"size is %f",ScrollView.contentSize.height);

我是这个领域的初学者。我已经应用了所有这些东西,我想在滚动视图上添加 webview 数据之后,滚动视图上的内容长度是多少,因为我定义了 2500 高度,然后覆盖了 1000,1500

【问题讨论】:

    标签: iphone ios objective-c uiwebview uiscrollview


    【解决方案1】:

    您可以使用以下方法找到滚动视图高度

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        int contentHeight = myWebView.scrollView.contentSize.height;
      Nslog(@"%d",contentHeight);
    }
    

    【讨论】:

      【解决方案2】:

      网页视图内容的正确高度只有在网页视图本身完成加载内容后才能获得。

      在您的情况下,您试图在 Web 视图完成加载之前获取高度。这是因为网页视图的加载是异步的。

      尝试实现 web 视图的委托 webViewDidFinishLoad 并从那里获取高度。在 Web 视图完成内容加载后调用此委托方法。

      - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
          //I usually use the web view scrollView.contentSize.height
      }
      

      不要忘记在视图控制器的头文件中声明视图控制器实现了 UIWebViewDelegate 协议。

      @interface RootViewController : UIViewController <UIWebViewDelegate>
      

      然后将web view的delegate设置为view controller:

      webView.delegate = self;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多