0

I am new in iOS programming and I am trying to have a view in storyboard with a UIImageView on top and a uiWebView below it. I set a UIScrollView that contains both of them and I would like that the scroll make the image disappearing while going down.

I follow this answer (link) and I got the scroll working but the UIWebView does not resize itself properly. I have tried many solutions around, but if I get the web view resizing I do not get scroll working anymore and vice versa.

Particularly, I am using:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _textDetailNews.delegate = self; // the web view delegate
    _textDetailNews.scrollView.scrollEnabled = NO;
    [_textDetailNews loadHTMLString:description baseURL:nil]; // set text to be shown in web view
}

and

- (void)webViewDidFinishLoad:(UIWebView *)webview 
{
   CGFloat height = [[_textDetailNews stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];

   [_scrollView setContentSize: CGSizeMake(_scrollView.frame.size.width, height + _scrollView.frame.size.height)];

   CGRect oldBounds = [[self textDetailNews] frame];
   [_textDetailNews setFrame:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, oldBounds.size.width, height)];
}

Where _scrollView already contains the UIImageView and the UIWebView set from storyboard.

Please, I appreciate any help for make the scrolling and UIWebView height resizing working.

EDIT

I am trying to make it more clear. The UIScrollView works fine with the above code but the UIWebView does not change its frame. While if I do not call: setContentSize on _srollView the UIWebView change size but the view does not scroll anymore.

Can somebody told me where is the issue?

Community
  • 1
  • 1
user2497897
  • 45
  • 2
  • 6

4 Answers4

1

You must delegate the "UIScrollViewDelegate" protocol. And in .m file you must implement the methods:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale

for example:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
    return scrollView.subviews.firstObject;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{

}
hiseh
  • 36
  • 2
  • Thanks for you replay, I've tried but it is not working. My view is still scrolling buy the web view is not resigning. So, scrolling down result into an empty view. Do you have other hints? – user2497897 Jul 28 '14 at 13:48
  • are you sure the UIScrollView is on the top? – hiseh Jul 28 '14 at 15:00
  • Perhaps it behinds some sibling views. You can use setZoomScale method to resize scrollView - (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated – hiseh Jul 28 '14 at 15:06
0

Since you are telling that you tried many permutations. So i am giving you one approach not an actual answer

I have worked that kind of application, And i was facing issue like you said, So i have used UITableView instead of UIScrollView and add UIWebView as a separate UITableViewCell.

Hope this will help a bit.

Kampai
  • 22,848
  • 21
  • 95
  • 95
  • thanks, it seems to be a good option. Anyway, I am wandering if the problem would remain since apple documentation suggest that the problem of double scrolling objects occurs within UIWebView inside an UIScrollView or UITableView. – user2497897 Jul 28 '14 at 13:54
  • You have to choose option between two that either you have to scroll UIWebView or its container view. So that at a time only one view will scroll. – Kampai Jul 28 '14 at 13:59
0

If you are embedding an UIWebView in an UIScrollView then perhaps you are running into the warning posted in the Class Reference: https://developer.apple.com/library/ios/documentation/Uikit/reference/UIWebView_Class/index.html

IMPORTANT You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

If you only need to include an image on-top of the UIWebView, is it possible to embed the image into the HTML?

Similar to: Using HTML and Local Images Within UIWebView

Community
  • 1
  • 1
0

I was stuck with this problem from a very long time. I also used the same link you used,Put a UIview and webview inside scrollview,still it was not working properly.webview's contentsize was wrong. I solved it by putting the webview inside another uiview then inside uiscroll view,not directly inside scrollview. hope that helps.

Siddhant Saxena
  • 109
  • 1
  • 4