0

I am trying to add native UIDatePicker a String option which which will represent the option for Ongoing, like job start-end intervals. However could not manage to find something.

I tried to create a custom view with UIButton and UIDatePicker and use it as inputView for UITextField, but when I tap the UITextField it becomes the responder and no inputview.

class DatePickerView: UIView {
    
    private lazy var datePicker: UIDatePicker = {
        let picker = UIDatePicker()
        picker.datePickerMode = .date
        picker.translatesAutoresizingMaskIntoConstraints = false
        return picker
    }()
    
    private lazy var ongoingButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .clear
        button.setTitleColor(.blue, for: .normal)
        button.setTitle("Ongoing", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.titleLabel?.font = .systemFont(ofSize: 18)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(ongoingButton)
        addSubview(datePicker)
        
        ongoingButton.topAnchor.constraint(equalTo: topAnchor).isActive = true
        ongoingButton.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        ongoingButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        ongoingButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
        
        datePicker.topAnchor.constraint(equalTo: ongoingButton.bottomAnchor).isActive = true
        datePicker.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        datePicker.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        datePicker.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        
        heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
}

Any suggestions?

Faruk
  • 2,269
  • 31
  • 42
  • Beyond a "kludge" of having the user pick an unrealistic date (say, 01/01/1900) I think you're on the right track - forcing the user to pick *either* a date *or* something else. What I'm not seeing is the sequence you are trying. Is the user tapping a text field and you wish to bring up the date picker and button? Instead of that, why not bring up your custom view and include the text field, sending back a formatted date or the contents of the text field. Maybe replace your button with a segmented control, showing the picker or a text field. Then the return can simply display what you need. –  Nov 08 '20 at 20:12
  • I will handle the unrealistic dates later on, need to talk with the team about it. This code is not responsible for getting the data directly. I am trying to use this as replacement for the keyboard of a textfield. UI is designed that way, no other way works for me actually. And I know this can be solved somehow. – Faruk Nov 08 '20 at 20:16
  • I saw similar design before in an app but I can not remember which – Faruk Nov 08 '20 at 20:17
  • What I'm describing (moving *all* editing of start/end date to the custom view) is definitely more "transactional" than most things you see in iOS. But the functionality you want is very much suited to that. Good luck! –  Nov 08 '20 at 20:22

0 Answers0