简体   繁体   中英

UIPageViewController Scroll transition affects webView content loading

I have a UIPageViewController that contains 4 views. Each of these views has the following layout: 在此处输入图片说明 Each view can be accessed as the initial view controller also know as pageViewController.currentPage . Each of these 4 views contains a UIWebView that loads it's HTML content in the init method. The UIWebViews are all the width of their view, 320. I just need them to match the height of the given content, so that I can scroll all the way down through the content. This actually works perfectly for the initial viewController . I sized the webView in the webViewDidLoad method like so:

self.scrollView.clipsToBounds = NO;
aWebView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
//NSLog(@"THIS IS WEBVIEWDIDFINISHLOAD IN ACTION");
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;

CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
scrollViewHeight = scrollViewHeight - self.articleTitleLabel.frame.size.height;
[self.scrollView setContentSize:(CGSizeMake(screenWidth, scrollViewHeight))];

CGSize fullContentSize = CGSizeMake(screenWidth, scrollViewHeight);
[self.scrollView setContentSize:fullContentSize];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenWidth, scrollViewHeight)];

aWebView.scrollView.scrollEnabled= NO;

THE PROBLEM is that when you swipe to another viewController , that UIWebView is not the adequate height, and the web content is cutoff, and the UIScrollview is the height of the screen.

***EXCEPTION: This is really weird, but it may help someone to answer the question: If you swipe horizontally as soon as the initial ViewController appear and go to the other views, their webViews will load perfectly with the correct height. The problem still remains that if you load up a ViewController , and take your time to read the content on that page, then swipe to another viewController , the webView will be cut off.

I ended up using Solution 3 from here and making the Scrollview fit my views: UIWebView Doesn't Load Completely, Height Varies

CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
CGRect frame = aWebView.frame;
frame.size.height = height + 130.0;
frame.size.width = width;
aWebView.frame = frame;

//Scrollview height for my views
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
self.scrollView.contentSize=CGSizeMake(screenWidth, self.articleImgView.frame.size.height+self.articleContentWebView.frame.size.height);

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