2011-07-14 46 views
2

我有一個UIPickerView在UITableViewController中上下動畫。我改編了一個來自蘋果(DateCell)的例子。 UIPickerView是以編程方式創建的,沒有nib文件。在橫向模式下不旋轉的動畫UIPickerView

在肖像模式下,一切看起來不錯。當我旋轉模擬器(我沒有在設備上測試)時,UITableView旋轉的很好,但選擇器保持原樣。我閱讀了大量有關該主題的主題,許多開發人員似乎在橫向模式下表現怪異的採摘員出現問題,但至少他們的採摘員正在旋轉。我做了這個鏈接中描述的UIPickerView的子類:http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode,但它沒有幫助旋轉問題。

我試圖用轉換旋轉選取器,但它看起來很奇怪,像破碎。

我懷疑問題是我使用UITableViewController內部的選擇器,所以我將它添加爲self.view.window的子視圖。如果我嘗試添加picker作爲self.view的子視圖,則只會出現一個白色框架(沒有任何選擇器)。

有什麼建議嗎?

在的UITableViewController子類的初始化代碼:如上述的鏈接描述UIPicker亞類的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.pickerView.superview == nil) 
    { 
     //Initialization code 
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     CGRect initFrame = orientation == UIInterfaceOrientationPortrait ? CGRectMake(0, 0, 320, 200) : CGRectMake(0, 0, 480, 160); 

     self.pickerView = [[RotatingUIPickerView alloc] initWithFrame:initFrame]; 
     [self.pickerView setDelegate:self]; 
     [self.pickerView setShowsSelectionIndicator:YES]; 
     [self.pickerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; 

     [self.view.window addSubview:self.pickerView]; 

     // compute the start frame 
     CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; 
     CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero]; 
     CGRect startRect = CGRectMake(0.0, 
             screenRect.origin.y + screenRect.size.height, 
             pickerSize.width, pickerSize.height); 

     [self.pickerView setFrame:startRect]; 

     // compute the end frame 
     CGRect pickerRect = CGRectMake(0.0, 
             screenRect.origin.y + screenRect.size.height - pickerSize.height, 
             pickerSize.width, 
             pickerSize.height); 

     // start the slide up animation 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 

     // we need to perform some post operations after the animation is complete 
     [UIView setAnimationDelegate:self]; 

     [self.pickerView setFrame:pickerRect]; 

     [UIView commitAnimations]; 
    } 
} 

實現:

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self == [super initWithFrame:frame]) 
    { 
     for (UIView * subview in self.subviews) 
     { 
      [subview setFrame:self.bounds]; 
     } 
    } 
    return self; 
} 

- (id) initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self == [super initWithCoder: aDecoder]) 
    { 
     for (UIView * subview in self.subviews) 
     { 
      [subview setFrame:self.bounds]; 
     } 
    } 
    return self; 
} 

回答

4

通過執行:

[self.view.window addSubview:self.pickerView]; 

要添加pickerView作爲您的子視圖UIWindow。如果可以的話,我不知道主要的UIWindow可以旋轉。

當您添加pickerView到視圖

[self.view addSubview:self.pickerView]; 

你,由於是一個UITableView視圖的問題。

我建議將pickerView添加到某些中間件UIView,作爲子視圖添加到UIWindow並添加到UITableView中。該中間體UIView也會有一個UIViewController與之關聯,因此shouldAutorotateToInterfaceOrientation可以返回正確的值以使自動旋轉工作。

+0

我懷疑它確實與我添加pickerView到UIWindow的事實有關。當然,這是有道理的!我按照你的建議做了一個快速的虛擬項目,並且完美地工作。非常感謝! – strave

+0

我使用與OP相同的代碼,不記得我找到了哪個教程,但遇到了同樣的問題(當然),這也解決了它的問題。唯一的區別是我使用的是tabbarcontroller,所以爲了正確地添加選擇器,將我的行更改爲'[self.tabBarController.view addSubview:self.pickerView];' – casey

+0

創建一箇中間'UIView',作爲子視圖添加到'UIWindow'和其中包含'UITableView':http://geekswithblogs.net/samerpaul/archive/2011/04/20/uitableview-and-uiviewcontroller-as-a-subview.aspx – To1ne

相關問題