Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Can I make a UIDatePicker appear on top of a UIKeyboard?

What I'm trying to do:

I've got a standard UITableView, with several UITextFields and a few cells showing a time/date. Basically, it's a form where users can input both text and dates.

How it's meant to work:

  • At first, neither the keyboard or date picker is shown.
  • If a text field is selected first, the keyboard pops up. Vice versa for a date picker (which pops up with a similar animation).
  • If originally a text field was selected, and then the user selects a time cell, the UIKayboard should turn into a date picker (no animation). Likewise should happen when the other way around.

What's not working:

Everything works great - except for the last step I listed above. Basically, I can't figure out how to present anything on top of the keyboard.

My method was just to animate both the keyboard/date up at the same time, but set the 'hidden' property for the date picker accordingly.

-

Any ideas as to how I might add something in front of the keyboard? Could I create another window on top of everything just after adding the keyboard? Or, use the accessoryView property but transpose the frame somehow?

Otherwise, are there any alternate ways I could achieve my original goal?

Thanks :)

like image 770
Jordan Smith Avatar asked Feb 23 '23 17:02

Jordan Smith


1 Answers

Even easier than listening for the keyboard notifications is to realize that UITextField is a subclass of UIResponder, which means it has an inputView property. You can simply create a UIDatePicker, set it to be your textfield's inputView, and you're done. (plus anything you need to do to actually extract the date from the picker).

The only downside of this approach is that while it works extremely well for the portrait orientation on iPhone, you'll need to do a bit of work getting things to look right in landscape and on the iPad. However, it's the same sort of stuff you'd have to do anyway.

like image 169
Dave DeLong Avatar answered Feb 25 '23 08:02

Dave DeLong