2011-12-01 89 views
1

這聽起來可能是一個新手的問​​題,但是我是新來的iOS開發,的UITableView不會對我的iPad上查看更改正確尺寸的設備方向變化

我有一個UIWebView和UITableView的。 在shouldAutorotateToInterfaceOrientation我調整它們的外觀,就像這樣。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {  
     if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ 
      CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2); 
      self.mTextView.frame = f; 
      f = CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2); 
      self.mTableView.frame = f; 
     } 
     if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
      CGRect f = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height); 
      self.mTextView.frame = f; 
      f = CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height); 
      self.mTableView.frame = f;   
     } 
     return YES; 
    } 

現在的問題: 對於表視圖繪製具有正確尺寸,但是當我切換到肖像模式然後再返回到橫向模式的UITableView變得比幀中指定較寬的第一負載。那麼爲什麼會發生這種情況以及如何解決這個問題

PS。我也試着用相同的方法設置所有單元格的幀並沒有幫助。

PSS。這種情況僅在設備上正確顯示模擬器的UITableView

回答

4

shouldAutorotateToInterfaceOrientation:僅返回指示視圖控制器是否支持指定方向的布爾值,你不應該做任何UI改造這種方法,而是盡一切willAnimateRotationToInterfaceOrientation:duration:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{  
    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ 
     CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2); 
     self.mTextView.frame = f; 
     f = CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2); 
     self.mTableView.frame = f; 
    } 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
     CGRect f = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height); 
     self.mTextView.frame = f; 
     f = CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height); 
     self.mTableView.frame = f;   
    } 
} 

Have a look at the documentation for UIViewController,這是很好的解釋。

+0

謝謝!真是虛擬的問題! – deimus

0

請檢查autoResizing屬性的意見。

2

iOS只調用一次該函數!換做是你想到這個工作不錯,你必須加入這一行到(void)viewDidLoad功能:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,使(void)orientationChanged:(id)object功能:

- (void)orientationChanged:(id)object{ 
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation; 

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){ 
     // some works 
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){ 
     // another works 
    } 
} 

我希望對您有用!

0

最簡單的方法是,您可以爲縱向和橫向模式創建2個不同視圖,因此每當模式更改時都可以輕鬆更改視圖。 像這樣:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
     self.view = self.portraitView; 
    else 
     self.view = self.landscapeView; 
    [tblScore reloadData]; 
    return YES; 
} 
2

您可以在縱向和橫向模式改變的UITableView的大小。 UITableView自動以相對於之前的方向模式的比例改變尺寸。所以你可以爲兩種模式修復所需的尺寸。

希望爲你工作。