14.1 C
Friday, October 20, 2023

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

Searching for some perception on this. If we for some purpose nonetheless can not use .inline model this manner, 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

Nevertheless, I received person requests to vary to the visible calendar-style .inline date picker, and I agree that is the appropriate approach to go. I take advantage of this picker to permit customers to outline a due date on initiatives and duties in a to-do listing app.

Sadly, the picker doesn’t push the remainder of the window contents up the best way the .wheels model does. As an alternative, it reveals as a tiny sliver on the backside of the display. 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 primary degree, nevertheless it’s damaged sufficient I can not launch it.

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

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

person 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 model as an inputView, however I have never been capable of finding official affirmation of this or a very good purpose why. Each different picker view in my app features this manner 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 info in a story manner to assist customers perceive tips on how 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)
    textField.inputView = picker
    let inputAccessory = picker.createInputAccessoryViewToolbar(inputView: textField)
    textField.inputAccessoryView = inputAccessory

The next is named by viewDidLoad() within the view controller:

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

I believed 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 to this point:

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

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

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

Plus lots of Googling.

Latest news
Related news


Please enter your comment!
Please enter your name here