Search code examples
iosswiftuipickerview

How to display only one row in UIPickerView?


I have a UIPickerView with a list of currencies to choose from.

I would like the picker to remain very short, like a typical dropdown menu in HTML, until the picker is clicked on.

When I try to shorten the height to about 12 pixels, iOS still displays it at a minimum of about 161 px at runtime.

I would basically like to display only one option until the component is clicked on, at which point the entire menu would show and the user could scroll through it, select the component, and then the picker wheel would hide again and only one component would be shown.

Any ideas on how this can be done in Swift?


Solution

  • I would like the picker to remain very short, like a typical dropdown menu in HTML, until the picker is clicked on

    Well, it isn't going to, so stop wanting that. (As the Stoics said, adjust your desires to fit reality and you'll be a much happier person.) A UIPickerView has a fixed minimum height - as you've already correctly stated. So you can't do what you're describing by simple manipulation of a UIPickerView.

    The solution, therefore, is to look for some other approach entirely. I suppose, for example, that you could remove the UIPickerView from the interface and replacing it with something else, such a label, describing what was last chosen; this is not so different from how the Timer view of Apple's Clock app works, where a picker (a date picker) is used to change the time limit, but a label is used to display the elapsed time.