2012-07-25 39 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