18

How can I programatically obtain the default height of an UIPickerView instance, in accordance to the resolution and orientation of the device that the app is currently running on?

I would like not to use a hardcoded value for this parameter, in the event that new devices will support different screen resolutions and thus will determine this component to have a different default size.

luvieere
  • 37,065
  • 18
  • 127
  • 179

5 Answers5

16

The warning that André saw doesn't seem to happen anymore (Xcode 4.2; iOS SDK 5.0). If you create it with initWithFrame and give it a height of zero, you can then read back it's height from the frame.height property:

uiPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 77, 320, 0)];
DLog(@"uiPickerView=%@",uiPickerView);

gives:

uiPickerView=<UIPickerView: 0x9a28c40; frame = (0 77; 320 216); layer = <CALayer: 0x9a28d00>>

So 216 is the default height.

I couldn't find a definition for this in the headers, so reading it back seems to be the safest way.

It is possible to set a frame with a non-zero height, but Apple say don't do that, and it seems to cause rendering problems.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
DavidA
  • 3,112
  • 24
  • 35
  • on an iPhone in portrait, 216 is still correct (as of iOS 10 and iPhone 7). In landscape, however, it's 162 when I print the frame. – user1687195 Aug 04 '17 at 20:25
3

With auto layout I was wondering the same due to the fact that you can't pin to the bottom of it.

The default height with no changes to an UIPickerView is 216px** UIPickerView**

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
2

If you create an instance with a height of 0, it will be overridden with the appropriate default height, and you can just get its frame.size.height.

However, on iPad this will issue a warning (tested a few days ago). I eventually had to use hardcoded values for iPad...

P.S. This was for a UIDatePicker; not sure if it's the exact same thing for UIPickerView.

André Morujão
  • 6,963
  • 6
  • 31
  • 41
  • Is there any possible way that doesn't issue warnings? It seems rather hacky to resort to a system coercion for adding a component that is recommended to have a default size. – luvieere May 02 '11 at 10:57
  • Just for clarification, this "warning" I mentioned is simply a message that the system prints to the console. This warning includes the suggested default height, which was the hardcoded value I ended up setting on my code. If you do that, you don't get any warnings. An like I said, if you set the default to 0 (and then read the value set by the system) you won't get these warnings on iPhone/iPod, it's only on iPad. – André Morujão May 02 '11 at 11:12
  • I received the warning messages on iPhone too, on iOS 4.2, informing me that the size of the control has been defaulted to its proper size. – luvieere May 02 '11 at 12:05
1

In 2022 it is 162. Pic is not touching the height just pulling into IB enter image description here

devjme
  • 684
  • 6
  • 12
0

You can use the frame property of the uipickerview.

Praveen S
  • 10,355
  • 2
  • 43
  • 69