ios – Is there a clear means to make use of .inline UIDatePicker() to populate UITextField() contents?

Spread the love


In search of some perception on this. If we for some motive nonetheless can not use .inline fashion this fashion, I ponder what the cleanest choice is for apps like mine that populate a textual content discipline based mostly on UIDatePicker() enter.

I am utilizing UIDatePicker() because the inputView for UITextField(). This labored nice in older iOS utilizing the .wheels UIDatePickerStyle(). The picker would push up from the underside to show rather than the keyboard, because it nonetheless does with different pickers I take advantage of elsewhere:
UIPickerView() as UITextField() inputView

Nonetheless, I received consumer requests to alter to the visible calendar-style .inline date picker, and I agree that is the fitting approach to go. I take advantage of this picker to permit customers to outline a due date on tasks and duties in a to-do checklist app.

Sadly, the picker doesn’t push the remainder of the window contents up the best way the .wheels fashion does. As a substitute, it exhibits as a tiny sliver on the backside of the display screen. You possibly can nonetheless faucet on the date (barely seen on the backside) and the calendar picker will overlay accurately. In different phrases, the picker is usable at a fundamental degree, however it’s damaged sufficient I am unable to launch it.

begin: textual content discipline reads “this venture will not expire”

consumer faucets on textual content discipline: UIDatePicker() scrunched/truncated at very backside of display screen

consumer faucets on date: picker overlay seems as anticipated

end: textual content discipline reads “expires November 22, 2023”

Tapping the X to clear the textual content discipline will return show state to “begin” in that sequence.

I’ve heard that Apple suggests not utilizing the .inline fashion as an inputView, however I have never been capable of finding official affirmation of this or a very good motive why. Each different picker view in my app capabilities this fashion to offer enter for textual content fields. I do not need to introduce a black sheep with UIDatePicker(). Additionally, as my screenshots display, I take advantage of the contents of the UITextField() to show data in a story means to assist customers perceive the best way to handle their duties.

Within the above screenshots, this class gives a customized date picker to be added to any UITextField():

class ExpirationDatePickerGenerator {
  
  static var pickerStyle: UIDatePickerStyle = .inline
  
  static func instantiateExpirationDatePicker(for textField: UITextField, in view: ExpirationDatePickerDelegate) {
    let picker = UIDatePicker()
    picker.datePickerMode = .date
    let calendar = Calendar.autoupdatingCurrent
    picker.minimumDate = calendar.date(byAdding: .day, worth: 1, to: Date())
    picker.addTarget(view, motion: #selector(view.userDidSelectDate(_:)), for: .valueChanged)
    picker.sizeToFit()
    textField.inputView = picker
    let inputAccessory = picker.createInputAccessoryViewToolbar(inputView: textField)
    textField.inputAccessoryView = inputAccessory
  }
}

The next known as by viewDidLoad() within the view controller:

ExpirationDatePickerGenerator.instantiateExpirationDatePicker(for: expirationDateTextField, in: self)

I assumed I used to be onto one thing calling picker.sizeToFit() earlier than assigning it because the textField.inputView however no cube. Has anybody gotten this to work as a textual content discipline enter?

Troubleshooting up to now:

I’ve tried defining a customized body for the picker within the generator, per a suggestion in an analogous thread:

let pickerFrame = CGRect(x: textField.body.minX, y: textField.body.minY, width: textField.body.width, top: 150)

I additionally added picker.sizeToFit() earlier than assigning to textField.inputView.

Plus a whole lot of Googling.

Leave a Reply

Your email address will not be published. Required fields are marked *