简体   繁体   中英

Frame of UIWebView inside UIScrollView does not stretch size to fit all of contents

In a small app that is loading web content from a blog, the UIWebView (which is inside a UIScrollView to be scrollable) will not scroll all of the content into view if it is longer than the actual UIScrollView's frame. Here is an image of the problem:

http://i.imgur.com/G4G4C.png

The code that loads the UIScrollView and the UIWebView (this is part of a UIViewController)

- (id)initWithHTML:(NSString*)html
{
    self = [super init];
    if (self) {
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
        UIScrollView* v = [[UIScrollView alloc] initWithFrame:appFrame];
        self.view = v;

        UIWebView* webView = [[UIWebView alloc] initWithFrame:v.frame];
        webView.userInteractionEnabled = NO;
        webView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:webView];

        [webView loadHTMLString:html baseURL:nil];
        CGSize sz = v.bounds.size;
        sz.height = webView.bounds.size.height + 20;
        v.contentSize = sz;        

        [v release];
        [webView release];
    }
    return self;
}

From Apple's documentation:

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.

It could be that when you are scrolling the events are getting mixed up. This could be the reason why its preventing you from scrolling the rest of the content into view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM