【发布时间】:2011-10-05 16:34:50
【问题描述】:
我有一个在 UITableViewController 中上下动画的 UIPickerView。我改编了苹果(DateCell)的一个例子。 UIPickerView 以编程方式创建,没有 nib 文件。
在纵向模式下,一切看起来都不错。当我旋转模拟器(我没有在设备上测试)时,UITableView 旋转得很好,但选择器仍然是原来的样子。我阅读了大量关于该主题的主题,许多开发人员似乎对选择器在横向模式下的行为怪异有问题,但至少他们的选择器会旋转。我创建了一个 UIPickerView 的子类,如以下链接所述:http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode,但它对旋转问题没有帮助。
我尝试使用变换来旋转选择器,但它看起来很奇怪,就像坏了一样。
我怀疑问题在于我在 UITableViewController 中使用了选取器,因此我将其添加为 self.view.window 的子视图。如果我尝试将选取器添加为 self.view 的子视图,则只会出现一个白框(没有任何选取器)。
有什么建议吗?
UITableViewController子类中的初始化代码:
- (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];
}
}
上面链接中描述的 UIPicker 子类的实现:
- (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;
}
【问题讨论】:
标签: iphone objective-c ipad uipickerview