3

I am looking to resize a UIPickerView / UIDatePickerView's height to 162 when my app is in landscape view. Is there an easy way to do this in auto layout in code?

I am currently trying the following method of handling the 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);
}

But when this code executes in the landscape orientation, I get the following effect where the picker is not bound to the bottom left of the screen anymore (this is the white margin that appears below it):

enter image description here

Is there a way or auto layout to handle this resizing with UIPickerViews, or should I just play around with the bounding rectangle during one of the view rotation methods in my view controller?

linusthe3rd
  • 3,455
  • 3
  • 28
  • 43
  • As far as I know, you can't change the size of a date picker. trying to change its size, either in IB or in code, just moves the y origin instead. – rdelmar Jul 08 '13 at 01:32
  • Actually, you are able to change the height of these views, but only the values of 162, 180, and 216 are acceptable. See this SO answer for reference: http://stackoverflow.com/a/6781755/89342 – linusthe3rd Jul 08 '13 at 01:35

2 Answers2

16

You are able to change the heights of the UIPickerView / UIDatePickerView, but only to the following values:

  • 162
  • 180
  • 216
linusthe3rd
  • 3,455
  • 3
  • 28
  • 43
1

If you are using Autolayout:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        self.dateHeightConstraint.constant = 216;
    } else {
        self.dateHeightConstraint.constant = 162;
    }
}

If you are not using Autolayout, then set the frame to have a height of 162 or 216 in this method.

Jeffrey Sun
  • 7,789
  • 1
  • 24
  • 17