1
 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

4

2 回答 2

2

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

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    int contentHeight = myWebView.scrollView.contentSize.height;
  Nslog(@"%d",contentHeight);
}
于 2013-06-04T08:26:55.957 回答
1

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

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

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

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

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

@interface RootViewController : UIViewController <UIWebViewDelegate>

然后将 Web 视图的委托设置为视图控制器:

webView.delegate = self;
于 2013-06-04T05:13:33.327 回答