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

I am beginner in this field . I have applied all these thing, I want after add webview data on scroll view then what the length of content on scroll view like as I have define 2500 hight then how much cover 1000,1500

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Rahul Sharma
  • 940
  • 2
  • 12
  • 31

2 Answers2

2

You can find scrollview height using following method

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    int contentHeight = myWebView.scrollView.contentSize.height;
  Nslog(@"%d",contentHeight);
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Dipak Narigara
  • 1,796
  • 16
  • 18
1

The correct height of the web view content can only be obtained after the web view has finished loading the content itself.

In your case, you are trying to get the height before the web view has finished loading. This is so because the loading of web view is asynchronous.

Try to implement the delegate of web view called webViewDidFinishLoad and get the height from there. This delegate method is called after the web view has finished loading the content.

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

Do not forget to declare that the view controller implements the UIWebViewDelegate protocol in the view controller's header file.

@interface RootViewController : UIViewController <UIWebViewDelegate>

And then set the delegate of the web view to the view controller:

webView.delegate = self;
Valent Richie
  • 5,226
  • 1
  • 20
  • 21