Adding UIDatePicker a non-Date option

48 Views Asked by At

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?

0

There are 0 best solutions below