Question

I've looked at numerous questions similar to this but try as I might, I cannot get a UIWebView to scroll or interact in any way. Here is the code:

- (void)loadView {
    [super loadView];

    ...

    [self.displayView loadHTMLString:entry->infoHtml baseURL:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.displayView.userInteractionEnabled = YES;
    self.displayView.scrollView.showsVerticalScrollIndicator = YES;
    self.displayView.scrollView.scrollEnabled = YES;
    [self.displayView.scrollView flashScrollIndicators];
}

The view appears, displays all the correct content, and shows the vertical scroll-bar for a second, correctly indicating the portion of the content currently displayed.

But I can't scroll it. Touching the content and moving my finger around does nothing at all. Interacting with a non-overlapping UITableView under the same parent works fine; touching entries there updates the contents of the UIWebView as it has been programmed to do.

The UIWebView was created in the Storyboard with these options:

  • Scales Page To Fit: unchecked (when checked, can't pinch-to-zoom, either)
  • Pagination: unpaginated
  • Mode: Aspect Fit (tried several; all un-scrollable)
  • UserInteractionEnabled: checked (parent views also have this checked)

The view is fully visible. I've tried making it significantly smaller than its parent and the displayed content always stops at the correct place. There is no delegate or gesture recognizers for this view.

The containing view has a UITableView and a UILabel, neither of which overlap the area of the UIWebView. I've tried changing the order of the views in the Scene on the storyboard but that also makes no difference.

I've tried removing the view from UIBuilder and creating it programmatically like so:

self.displayView = [[UIWebView alloc] initWithFrame:CGRectMake(400, 220, 400, 300)];
[self.view addSubview:self.displayView];

Same results. I tried removing all other elements from the Scene except for this single programmatically created UIWebView. Still the same results.

Elsewhere in my code, I've put a UIWebView as the @"accessoryView" of a UIAlertView and there I can scroll the content as expected.

Is there something else that needs to be done to be able to scroll a UIWebView sub-view?

2014-04-20: In fact, if I add the following lines to the bottom of my -loadView method, I get a dialog with the same HTML content as the window behind it, but I am able to scroll the dialog version. The only difference I can think of is that the dialog is modal (forced focus) while my own window with both a UITableView and UIWebView still allows interaction with the views visible behind it.

UIWebView* contents = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
[contents loadHTMLString:entry->infoHtml baseURL:nil];
[alertView setValue:contents forKey:@"accessoryView"];
[alertView show];

On the other hand, if I replace the last two lines of that with [self.view addSubview:contents]; then I get the same second HTML view in the upper-left corner of the screen but that will not scroll.

Was it helpful?

Solution

I've worked it out. First, I tried adding this to -viewDidLoad in my MainController class:

UIWebView* contents = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
[contents loadHTMLString:@"<html><head><title>FOO</title></head><body><p>A</p><p>B</p><p>C</p><p>D</p><p>E</p><p>F</p><p>G</p><p>H</p><p>I</p><p>J</p><p>K</p><p>L</p><p>M</p><p>N</p><p>O</p><p>P</p><p>Q</p><p>R</p><p>S</p><p>T</p><p>U</p><p>V</p><p>W</p><p>X</p><p>Y</p><p>Z</p></body></html>" baseURL:nil];
contents.backgroundColor = [UIColor redColor];
[self.view addSubview:contents];

It displays but isn't scrollable. I have a StartupController that deals with initializing the app and runs first so I added the same code there. This was scrollable! Ah-Ha!

It occurred to me that the top-level Google Map (GMSMapView) that covers the MainView might be swallowing the scroll events but passing click events. Within the scene, instead of just a single map-view, I created a top-level plain/simple/empty UIView and made the GMSMapView a full-sized child of that. Now when I add my views to that new top-level (simple) view, they are siblings rather than children of the map and get all events correctly.

As a result, my UIWebView is now scrollable. Thanks for everybody's comments and suggestions! It's been several months of on-and-off trying different things to finally figure that one out.

OTHER TIPS

Please check that the web view is contained fully within the bounds of its parent view (and so on up the view hierarchy). An easy way to do this is to set some background colors. Possibly you have some constraints that aren't behaving as you expect.

Similarly, that all parent views are user-interaction-enabled.

Edit: I understand that the web view is fully visible. But that doesn't mean it is contained within the bounds of its superview. If the superview has clipsToBounds=NO (default, I believe) then any subview outside its bounds will be visible but will not receive touches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top