我有一个webview,我已经添加到xib处于纵向模式。我已将xib的webview框架设置为(0,54,768,905)。当我以纵向模式运行我的应用程序时,它会在适当的框架中显示webview,但是当我从纵向旋转到横向时,我的webview框架没有正确设置。
在will rotate界面方向中,我设置了webview的框架,但没有反映出任何变化。当我从纵向旋转到横向时,webview的框架从y position = 0而不是54开始。
这是我的代码:
-(void)viewDidLoad
{
//Landscape
viewstatus =@"landscape";
if (webview)
{
[webview removeFromSuperview];
}
self.webview.frame =CGRectMake(0, self.view.origin.y+54, 1024, 680);
[self.view addSubview:webview];
NSURL *url = [NSURL fileURLWithPath:dataObject];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; 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
if (webview)
{
[webview removeFromSuperview];
}
self.webview.frame =CGRectMake(0, self.view.origin.y+54, 768, 905);
[self.view addSubview:webview];
NSURL *url = [NSURL fileURLWithPath:dataObject];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; 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.scrollEnabled=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;
pinchdetect=NO;
pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
[self.webview addGestureRecognizer:pinchRecognizer];
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == 1 || toInterfaceOrientation == 2)
{
viewstatus =@"portrait";
[self.view bringSubviewToFront:pagecontrol];
if (webview)
{
[webview removeFromSuperview];
}
self.webview.frame =CGRectMake(0, self.view.origin.y+54, 768, 905);
[self.view addSubview:webview];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 905.0f, 768.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
// webview.frame =CGRectMake(0, 54, 768, 905);
pagecontrol.frame =CGRectMake(230, 959, 600, 36);
}
if(toInterfaceOrientation == 3 || toInterfaceOrientation == 4)
{
viewstatus =@"landscape";
if (webview)
{
[webview removeFromSuperview];
}
self.webview.frame =CGRectMake(0, self.view.origin.y+54, 1024, 680);
[self.view addSubview:webview];
[self.view bringSubviewToFront:pagecontrol];
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 680.0f, 1024.0f];
[webview stringByEvaluatingJavaScriptFromString:insertRule1];
pagecontrol.frame =CGRectMake(300, 710, 150, 36);
}
[webview reload];
}
答案 0 :(得分:0)
首先使用下方法
将shouldAutorotateToInterfaceOrientation
设置为YES
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation
{
return YES;
}
然后添加此方法
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
[webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];
}
else{
[webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
}
}
答案 1 :(得分:0)
如果您使用自动布局和IB,当您将webview放到视图上时,请使用固定。将顶部,前部,尾部和底部固定到视图上。无论你使用什么方向,它都会坚持下去。我这样做了几次,效果很好。