Default UIPickerView height

安稳与你 提交于 2019-12-06 18:29:48

问题


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.


回答1:


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.




回答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.




回答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




回答4:


You can use the frame property of the uipickerview.



来源:https://stackoverflow.com/questions/5855717/default-uipickerview-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!