如何在横向旋转中使用自动布局调整UIPickerView/UIDatePickerView高度?

lin*_*3rd 3 uidatepicker uipickerview uikit ios autolayout

当我的应用程序处于横向视图时,我希望将UIPickerView/UIDatePickerView的高度调整为162.在代码中自动布局是否有一种简单的方法可以做到这一点?

我目前正在尝试以下处理resize的方法:

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                      duration:(NSTimeInterval)duration
{
    NSInteger height = 216;
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        height = 162;
    }

    datePicker.bounds =
        CGRectMake(datePicker.frame.origin.x, datePicker.frame.origin.x,
               datePicker.frame.size.width, height);
}
Run Code Online (Sandbox Code Playgroud)

但是当这个代码以横向方向执行时,我得到以下效果:选择器不再绑定到屏幕的左下角(这是它下面出现的白色边距):

在此输入图像描述

有没有办法或自动布局来处理UIPickerViews的调整大小,或者我应该在视图控制器中的一个视图旋转方法中使用边界矩形?

lin*_*3rd 15

您可以更改UIPickerView/UIDatePickerView的高度,但仅限于以下值:

  • 162
  • 180
  • 216