Redrawing content of UIWebView
Posted
by
btate
on Stack Overflow
See other posts from Stack Overflow
or by btate
Published on 2011-05-19T14:47:33Z
Indexed on
2012/04/11
23:29 UTC
Read the original article
Hit count: 173
I have a bunch of webviews with static html content that I'm putting in a scroll view as pages. That works fine, but having 20 something full screen subviews of the scroll view is causing some lag. I solved that by only placing 5 at a time in there. The current view, and the two next and two previous.
The problem now is that any web view that is not a subview of the scroll view is not drawing correctly based on the device orientation. The frame of the webview is printing out correct, but the actual content is drawing in portrait mode. So there is essentially a strip of blank space to the right of the content.
How do I go about re rendering the content without reloading the page?
Here's the relevant code:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation{
[self resizeSubViews];
}
- (void) setupWebViews{
if(_webViews == nil)
_webViews = [[NSMutableArray alloc] init];
// This is where the navigation would come into play as far as loading up available web views
[_webViews removeAllObjects];
// for loop here to create web views
for (int i = 0; i < 20; i++) {
//CDCWebViewController *webView = [[[MyInternalWebView alloc] initWithFrame:_webViewWrapper.frame] retain];
MyInternalWebView *page = [[[MyInternalWebView alloc] init] retain];
[page loadRequest:[NSURLRequest requestWithURL:_url]];
[page setCdcIWVdelegate:self];
[page setTestIndex:i];
[page setPageIndex:i];
[_webViews addObject:page];
[self loadScrollViewWithPage:i];
}
[self clearUnusedWebViews:_pageControl.currentPage];
}
- (void) setupWebViewWrapper{
// a page is the width of the scroll view
_webViewWrapper.pagingEnabled = YES;
_pageControl = [[[UIPageControl alloc] init] retain];
_webViewWrapper.showsHorizontalScrollIndicator = NO;
_webViewWrapper.showsVerticalScrollIndicator = NO;
_webViewWrapper.scrollsToTop = NO;
_webViewWrapper.delegate = self;
_pageControl.numberOfPages = [_webViews count];
_pageControl.currentPage = 0;
}
- (void) resizeSubViews{
// The frame is set in IB
_webViewWrapper.contentSize = CGSizeMake(_webViewWrapper.frame.size.width * [_webViews count], _webViewWrapper.frame.size.height);
// Move the content offset.
_webViewWrapper.contentOffset = CGPointMake(_webViewWrapper.frame.size.width * _pageControl.currentPage, _webViewWrapper.contentOffset.y);
for (MyInternalWebView *subview in _webViewWrapper.subviews) {
// Reset the frame height and width here?
CGRect frame = _webViewWrapper.frame;
frame.origin.x = frame.size.width * subview.pageIndex;
frame.origin.y = 0;
[subview setFrame:frame];
}
}
//*****************************************************
//*
//* ScrollView Functions
//*
//*****************************************************
- (void)loadScrollViewWithPage:(int)page {
// Make sure we're not out of bounds
if (page < 0) return;
if (page >= [_webViews count]) return;
MyInternalWebView *webView = [_webViews objectAtIndex:page];
// Add the preloaded webview to the scrollview if it's not there already
if (nil == [webView superview]) {
CGRect frame = _webViewWrapper.frame;
//NSLog(@"width = %f", frame.size.width);
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
//NSLog(@"setting frame for page %d %@", page, NSStringFromCGRect(frame));
[webView setFrame:frame];
[_webViewWrapper addSubview:[_webViews objectAtIndex:page]];
// Now that the new one is loaded, clear what doesn't need to be here
[self clearUnusedWebViews:page];
}
}
- (void) clearUnusedWebViews: (NSInteger) page{
for (int i = 0; i < [_webViews count]; i++) {
if ((page - i) <= 2 && i - page <= 2) {
continue;
}
[[_webViews objectAtIndex:i] removeFromSuperview];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = _webViewWrapper.frame.size.width;
NSInteger page = floor((_webViewWrapper.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
_pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self loadScrollViewWithPage:page + 2];
}
© Stack Overflow or respective owner