Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show UIPickerView like a keyboard, without UITextField

Tags:

ios

uikit

ios6

I'm looking for a way to present a UIPickerView when the user taps on a UIBarButtonItem. Imagine a filter for the table view results.

I know I could use a UITextField inputView, but this would not be the case -- all I have is a UIBarButtonItem and a UITableView.

I've seen into using a UIActionSheet, but it does not look natural, specially when it's animating to show.

Would animating the UIView on and off the screen manually the only option?

The app is iOS 6+ and iPhone only, so I don't need to keep compatibility with any other versions/idioms.

like image 560
leolobato Avatar asked Mar 18 '13 18:03

leolobato


1 Answers

Attach a UIPickerView as the inputView of a 0-sized UITextField that you have added to your view.

let picker = UIPickerView()
picker.dataSource = self
picker.delegate = self
        
let dummy = UITextField(frame: .zero)
view.addSubview(dummy)
        
dummy.inputView = picker
dummy.becomeFirstResponder()
like image 92
Michael Peterson Avatar answered Nov 12 '22 12:11

Michael Peterson