gpt4 book ai didi

iphone - 动画 UIPickerView 在横向模式下不旋转

转载 作者:行者123 更新时间:2023-12-03 20:46:03 25 4
gpt4 key购买 nike

我有一个 UIPickerView,它在 UITableViewController 中上下动画。我改编了苹果(DateCell)的一个例子。 UIPickerView 是以编程方式创建的,没有 nib 文件。

在肖像模式下,一切看起来都很好。当我旋转模拟器时(我没有在设备上进行测试),UITableView 旋转良好,但选择器仍然保持原样。我阅读了大量有关该主题的帖子,许多开发人员似乎都遇到了选择器在横向模式下行为怪异的问题,但至少他们的选择器会旋转。我按照此链接中的描述创建了 UIPickerView 的子类:http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode ,但这对解决轮换问题没有帮助。

我尝试使用变换来旋转选择器,但它看起来很奇怪,就像坏了一样。

我怀疑问题是我在 UITableViewController 内部使用了选择器,因此我将其添加为 self.view.window 的 subview 。如果我尝试将选择器添加为 self.view 的 subview ,则只会出现一个白色框架(没有任何选择器)。

有什么建议吗?

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;
}

最佳答案

通过执行:

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

您正在将 pickerView 添加为 UIWindow 的 subview 。我不知道主 UIWindow 如何旋转(如果可以的话)。

当您将 pickerView 添加到 View 时

 [self.view addSubview:self.pickerView];

由于 View 是 UITableView,您会遇到问题。

我建议将pickerView添加到一些中间UIView,作为 subview 添加到UIWindow并向其中添加UITableView。此中间 UIView 还将有一个与其关联的 UIViewController,以便 shouldAutorotateToInterfaceOrientation 可以返回正确的值以使自动旋转工作。

关于iphone - 动画 UIPickerView 在横向模式下不旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com