1

我有一个基于导航的应用程序,并说我的根控制器 A 导致 viewController B 被推送,它有一个 UITableView 和一些带有 UITextFields 的单元格。

前 2 个 UITextField 分别有一个 UIDatePicker 和一个 UIPickerView 作为它们的 inputView。此外,UIPickerView 有 4 个组件,其 UIViews 通过返回

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 37)];
    label.text = [NSString stringWithFormat:@"%@%i", ((component == 3) ? @"." : @""), row];
    label.textAlignment = UITextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:24];
    label.backgroundColor = [UIColor clearColor];
    [label autorelease];

    return label;
}

在垂直方向上,UIPickerView 如下所示: 在此处输入图像描述

我注意到以下内容:

案例 1 - 正确

  1. 我在垂直方向的 viewController A 中。
  2. 我将设备/模拟器从垂直方向旋转到水平方向
  3. 我推 viewController B
  4. 我导致 UIPickerView 弹出

选择器框架正确拉伸到横向模式,组件视图仍然是正确的大小并居中:

在此处输入图像描述

案例 2 - 不正确

  1. 我在垂直方向的 viewController A 中。
  2. 我推 viewController B
  3. 我将设备/模拟器从垂直方向旋转到水平方向
  4. 我导致 UIPickerView 弹出

在这种情况下,UIPickerView 的组件会变得混乱:

在此处输入图像描述

有什么想法吗?

4

2 回答 2

3

所以我想了这么多:不知道为什么,但是-setNeedsLayout当设备旋转时,输入视图基本上没有调用。

UIDatePicker (inputView = UIDatePicker) 会导致选择器自动(重新)将自己放置到正确的方向,但 UIPickerView (inputView = UIPickerView) 不会。

所以看起来 UIPickerView 可能没有注册方向更改或类似的东西。我通过手动注册解决了这个问题。也就是说,在我的设置代码中,我添加:

[[NSNotificationCenter defaultCenter] addObserver:weightPicker 
                                         selector:@selector(setNeedsLayout) 
                                             name:UIDeviceOrientationDidChangeNotification   
                                              object:nil];

现在选择器表现得很好。

另外,请记住在 textField 被释放之前向 NSNotificationCenter 取消注册。

于 2012-03-21T05:56:57.427 回答
1

作为记录,@SaldaVonSchwartz 自己的答案的稍微干净的版本:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  [myPickerView setNeedsLayout];
}

这告诉 myPickerView 您要调整 myPickerView 的子视图的布局。应该做的伎俩。

于 2015-10-27T20:08:44.283 回答