How to make an UIPickerView with a Done button?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The most common way to show a UIPickerView with a Done button is not to place both controls manually on the screen. In UIKit, the standard pattern is to use the picker as a text field's inputView and a UIToolbar with a Done button as the inputAccessoryView.
Why This Pattern Works Well
When a text field becomes first responder, iOS shows its input view just like it would show the keyboard. If the input view is a picker instead of a keyboard, the user gets a native bottom-sheet style selector, and the accessory toolbar appears above it.
That gives you:
- a familiar interaction model
- an easy place for Done and Cancel buttons
- automatic presentation and dismissal behavior through first responder handling
Complete Swift Example
This is the standard UIKit setup for a picker with a Done button.
What Each Part Does
- '
inputView: replaces the keyboard with the picker' - '
inputAccessoryView: provides the toolbar above the picker' - '
resignFirstResponder(): dismisses the picker and toolbar' - '
selectedRow(inComponent:): reads the current picker choice'
This separation is important. The picker chooses the value, and the toolbar controls the interaction lifecycle.
Adding a Cancel Button
Many apps use both Cancel and Done:
Then implement:
This is useful when you do not want to commit the selection automatically.
If the Picker Is Not Tied to a Text Field
You can also place a UIPickerView directly in your layout and put a toolbar or button above it with Auto Layout constraints. That works, but it is a different UI pattern. The inputView approach is usually preferred for form-like data entry because it behaves like a controlled replacement for the keyboard.
Choose based on the interaction:
- form field selection: use
inputView - always-visible picker area: place it directly in the view hierarchy
Delegate and Data Source Basics
A picker needs two categories of methods:
- data source methods for component count and row count
- delegate methods for titles or custom views and selection handling
The minimum setup for a basic string picker is:
- '
numberOfComponents' - '
numberOfRowsInComponent' - '
titleForRow'
You can also use didSelectRow if you want live updates before the user taps Done.
Common Pitfalls
The biggest mistake is creating the picker and toolbar but forgetting to assign them to inputView and inputAccessoryView. In that case, tapping the text field still shows the normal keyboard.
Another mistake is forgetting resignFirstResponder() in the Done action. Without it, the picker stays visible.
People also read the selected row before the picker has valid data, usually because the delegate or data source was never assigned.
Finally, if the text field should not allow typed text, do not forget that the picker is replacing keyboard input conceptually. Keep the interaction consistent and avoid mixing free typing with picker-only choices unless that is truly intended.
Summary
- The standard UIKit pattern is
UITextField.inputView = pickerView. - Add a
UIToolbarwith a Done button throughinputAccessoryView. - Use the Done action to read the selected row, update the field, and call
resignFirstResponder(). - Add Cancel if you want dismissal without committing the selection.
- Use a directly embedded picker only when the picker should remain visible on screen.
Related reading
- How to make ConstraintLayout work with percentage values?
- How to make custom dialog with rounded corners in android
- How to make EditText not editable through XML in Android?
- How to make gradient background in android
- How to make HTTP Post request with JSON body in Swift?
- How to make HTTP request in Swift?
- How to make HTTP request in Swift?
- How to make iPhone vibrate using Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.