How do I make a modal date picker that only covers half the screen?

rustybeanstalk picture rustybeanstalk · Dec 13, 2011 · Viewed 17k times · Source

I want to have the same effect as the birthday date setter in the iPhone contacts app. How do I have a modal date picker come up only halfway on the screen.

I want the date picker to be over a UITableView (like in the iphone contacts app). I would like the UITableView to scroll freely behind the date picker (like in the iphone contacts app).

Thanks in advance.

Answer

eladleb picture eladleb · Dec 21, 2011

By far, the best way to achieve a date-picker/uipicker keyboard-like is to use the input view property of a textfield:

  1. Build a simple custom UITableViewCell that contains a UITextField (It's important only because the question was about embedding one in a table view)

    @interface pickerCell : UITableViewCell
    {
        UITextField *txtTextField;
        UIPickerView* dtpPicker;
    }
    
  2. set the TextField's inputview to be the UIPickerView (Make sure they have both been allocated and initialized first):

    txtTextField.inputView = dtpPicker;
    
  3. You are almost done. Now when the textfield becomes the first responder (the user tapped it), the user will be presented with a picker view. Just fill it with your own values. or use a date picker.

  4. You can set the picker's delegate/datasource to the same cell (if it will always be a birthdate cell) or have the cell's superview load it with data. Whichever choice you make, be sure to update the txtTextField.text on

    pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    

Good luck! Elad.