【问题标题】:Animated UIPickerView not rotating in landscape mode动画 UIPickerView 不在横向模式下旋转
【发布时间】: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


    【解决方案1】:

    通过执行:

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

    您正在添加pickerView 作为UIWindow 的子视图。我不知道主要的UIWindow 是如何旋转的,如果可以的话。

    当您将pickerView 添加到视图时

     [self.view addSubview:self.pickerView];
    

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

    我建议将pickerView 添加到一些中间UIView,作为子视图添加到UIWindow 并添加UITableView。这个中间的UIView 也将有一个与之关联的UIViewController,以便shouldAutorotateToInterfaceOrientation 可以返回正确的值以使自动旋转工作。

    【讨论】:

    • 我怀疑这确实与我将 pickerView 添加到 UIWindow 的事实有关。当然,这是有道理的!我按照你的建议做了一个快速的虚拟项目,效果很好。非常感谢!
    • 我使用与 OP 相同的代码,不记得我在哪个教程上找到的,但我遇到了同样的问题(当然),这也解决了它。唯一的区别是我使用的是 tabbarcontroller,因此正确添加选择器将我的行更改为 [self.tabBarController.view addSubview: self.pickerView];
    • 创建一个中间UIView,作为子视图添加到UIWindow,其中包含UITableViewgeekswithblogs.net/samerpaul/archive/2011/04/20/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多