Webビュー内にHTMLページを表示しているWebビューを持つアプリケーションがあります。HTMLページのコンテンツがWebビューのフレームを超えると、Webビューのフレーム外の残りのコンテンツを新しいページに渡します。これは正常に動作しています。
Web ビューを縦向きモードで起動し、画面を横向きに回転すると、Web ビューのコンテンツが正しく表示されます。しかし、Web ビューのコンテンツを次のページにスクロールしてから Web ビューを回転すると、回転が正しく機能しません。1 ページ目の半分のコンテンツと 2 ページ目の半分のコンテンツが Web ビューに表示されます。
これは私のコードです:
- (void)viewDidLoad
{
[super viewDidLoad];
backbuttonclick=NO;
textFontSize=100;
pageControlBeingUsed = NO;
pagecontrol = [[StyledPageControl alloc] initWithFrame:CGRectMake(208, 959, 358, 36)];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
//Landscape
viewstatus =@"landscape";
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:20px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 680.0f, 1024.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
pagecontrol.frame =CGRectMake(300, 710, 150, 36);
}else{
//Portrait
viewstatus =@"portrait";
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:20px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 905.0f, 768.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
pagecontrol.frame =CGRectMake(208, 959, 358, 36);
}
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(handleDoubleTap:)];
doubleTap.numberOfTouchesRequired = 2 ;
[self.view addGestureRecognizer:doubleTap] ;
UIScrollView* sv = nil;
for(UIView* v in self.webview.subviews){
if([v isKindOfClass:[UIScrollView class] ]){
sv = (UIScrollView*) v;
[sv setZoomScale:9.0f animated:YES];
sv.bounces = NO;
}
}
[pagecontrol setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[pagecontrol addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pagecontrol];
webview.scrollView.delegate=self;
webview.scrollView.showsVerticalScrollIndicator=NO;
webview.scrollView.bounces = NO;
webview.scrollView.pagingEnabled = YES;
NSURL *url = [NSURL fileURLWithPath:dataObject];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
Webビューでロードが完了した場合、コンテンツサイズが作成されてからビューサイズが作成される場合、HTMLコンテンツをページに分割するCSSを作成しました:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
if (height<960)
{
pagecount=1;
}
if (height>960&&height<1920)
{
pagecount=2;
}
if (height>1920&&height<2880)
{
pagecount=3;
}
if (height>1024)
{
NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 ;
if([viewstatus isEqualToString:@"landscape"])
{
insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:10px; height: %fpx; -webkit-column-gap:0px; -webkit-column-width: %fpx;')",
680.0f, 1024.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
}
if([viewstatus isEqualToString:@"portrait"])
{
insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:10px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')",
905.0f, 768.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
}
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
}
int totalWidth = [[webview stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
int hi = (int)((float)totalWidth/webview.bounds.size.width);
self.pagecontrol.numberOfPages=hi;
self.pagecontrol.hidesForSinglePage=YES;
[self.pagecontrol setPageControlStyle:PageControlStyleStrokedCircle];
}
これは私のスクロール方法です:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (!pageControlBeingUsed)
{
self.webview.scrollView.showsHorizontalScrollIndicator=NO;
CGFloat pageWidth = self.webview.scrollView.frame.size.width;
int page = floor((self.webview.scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
NSLog(@"%d",page);
self.pagecontrol.currentPage = page;
[webview.superview setAutoresizesSubviews:YES];
webview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
return;
}
}
これは私の回転方法です:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == 1 || toInterfaceOrientation == 2)
{
viewstatus =@"portrait";
[self.view bringSubviewToFront:pagecontrol];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:20px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 905.0f, 768.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
[webview.superview setAutoresizesSubviews:YES];
webview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
pagecontrol.frame =CGRectMake(230, 959, 600, 36);
}
if(toInterfaceOrientation == 3 || toInterfaceOrientation == 4)
{
viewstatus =@"landscape";
[self.view bringSubviewToFront:pagecontrol];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px;margin-top:10px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 680.0f, 1024.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
[webview.superview setAutoresizesSubviews:YES];
webview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
pagecontrol.frame =CGRectMake(300, 710, 150, 36);
}
}