Default UIPickerView height

luvieere picture luvieere · May 2, 2011 · Viewed 18.7k times · Source

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.

Answer

DavidA picture DavidA · Nov 15, 2011

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.