2012-07-25 152 views
0

我有一个UIWebView只覆盖屏幕右侧的大部分。 为此,我使用frame属性为纵向定义了框架大小和位置。 webView.frame=CGRectMake(220,100,548,875);UIWebView大小旋转时不正确

当设备旋转为横向,我指定横向方向的新框架: webView.frame=CGRectMake(300,73,724,638);

问题是,当我再次旋转web视图从纵向到横向和纵向和我改变了框架原来的一个,它的宽度只有从之前一个半..

这里是我的代码和函数调用来调整框架:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) 
    { 
    } 
    else if(self.interfaceOrientation==UIInterfaceOrientationPortrait) 
    { 
     [self showLandscape]; 
    } 
    else if(toInterfaceOrientation==UIInterfaceOrientationPortrait) 
    { 
     [self showPortrait]; 
    } 
} 

-(void)showLandscape 
{ 
. 
. 
. 
webView.frame=CGRectMake(300,73,724,638); 
. 
. 
. 
} 

-(void)showPortrait 
{ 
. 
. 
. 
webView.frame=CGRectMake(220,100,548,875); 
. 
. 
. 
} 

如何解决这个问题?

+0

是'webView'动态创建的吗? – holex 2012-07-25 08:48:54

+0

是的webView是动态创建 – aswin 2012-07-25 08:54:46

+0

你可以采取一些截图吗? – holex 2012-07-25 09:00:58

回答

1

这是我做了什么,它运作良好

- (void)viewDidLoad { 
    UIWebView *_webView = [[UIWebView alloc] init]; 
    // when I add these line it is also working well (!!!) on my device 
    //_webView.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
    //_webView.autoresizesSubviews=YES; 
    //_webView.contentMode=UIViewContentModeScaleAspectFit; 
    [self.view addSubview:_webView]; 
    [_webView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)]; 
} 

,我已经添加了下面的代码,以及:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    for (UIView *_temporaryView in self.view.subviews) { 
     if ([_temporaryView isKindOfClass:[UIWebView class]]) { 
      if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){ 
       [_temporaryView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)]; 
      } else { 
       [_temporaryView setFrame:CGRectMake(300.f, 73.f, 724.f, 638.f)]; 
      } 
     } 
    } 

    return true; 
} 

请,将其与比较你的代码,并找到差异,如果你不想提供你的代码片段来找到异常。

+0

我删除了行 webView.autoresizingMask = UIViewAutoresizingFlexibleWidth; ,它已经工作。感谢您的回复。 :) – aswin 2012-07-25 10:15:43

+0

不客气。 :) – holex 2012-07-25 10:18:29