18.5 C
London
Sunday, September 15, 2024

ios – Methods to replace the maxDate of UIdatePicker after setting it as soon as?


I’ve a SwiftUI based mostly software. I’ve a Date Picker part which is definitely UIKit based mostly.

I’ve to set the maxDate of the date picker dynamically which is why I’ve a @Binding variable to replace the maxDate each time wanted.

However appears to be like prefer it does not work correctly. The maxDate by no means adjustments.
See beneath code. It conforms to UIViewRepresentable in order that I can in order that I can use UIKit.

import Basis
import SwiftUI

struct DatePickerTextField: UIViewRepresentable {
    personal let textField = UITextField()
    personal let datePicker = UIDatePicker()
    personal let helper = Helper()
    public var placeholder: String
    public var minDate: Date?
    public var maxDate: Date?
    @Binding public var bindableMaxDate: Date?
    @Binding public var date: Date?
    @Binding var didStartEditing: Bool
    func makeUIView(context: Context) -> UITextField {
        datePicker.datePickerMode = .date
        datePicker.minimumDate = minDate
        // datePicker.maximumDate = maxDate
        datePicker.maximumDate = bindableMaxDate?.dateByAddingDays(3)
        datePicker.preferredDatePickerStyle = .wheels
        datePicker.addTarget(self.helper, motion: #selector(self.helper.dateValueChanged), for: .valueChanged)
        textField.placeholder = placeholder
        textField.inputView = datePicker
        textField.backgroundColor = UIColor(Shade.TerOffWhite)
        textField.textColor = UIColor(Shade.TerDarkGrey)
        textField.layer.borderColor = UIColor(Shade.RadioButtonBorder).cgColor
        textField.layer.cornerRadius = 8
        textField.layer.borderWidth = 1
        textField.font = UIFont(identify: "HelveticaNeue-Daring", dimension: 14.0)
        let paddingView: UIView = UIView(body: CGRect(x: 0, y: 0, width: 10, top: 20))
        textField.leftView = paddingView
        textField.leftViewMode = .all the time
        let rightView = UIView(body: CGRect(x: 0, y: 0, width: 30, top: 20))
        // Create UIButton
        let myButton = UIButton(sort: .customized)
        myButton.body = CGRect(x: 0, y: 0, width: 20, top: 20)
        myButton.setImage(UIImage(named: "icon_calendar_input"), for: .regular)
        myButton.addTarget(self.helper, motion: #selector(self.helper.editingStarted), for: .touchUpInside)
        rightView.addSubview(myButton)
        textField.rightView = rightView
        textField.rightViewMode = .all the time
        // textField.borderStyle = .roundedRect
        let toolbar = UIToolbar()
        toolbar.sizeToFit()
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, goal: nil, motion: nil)
        let doneButton = UIBarButtonItem(title: "Carried out", model: .plain, goal: helper, motion: #selector(helper.doneButtonTapped))
        toolbar.setItems([flexibleSpace, doneButton], animated: true)
        textField.inputAccessoryView = toolbar
        helper.onDateValueChanged = {
            date = datePicker.date
        }
        helper.onDoneButtonTapped = {
            if date == nil {
                date = datePicker.date
            }
            textField.resignFirstResponder()
        }
        helper.onEditingStarted = {
            textField.becomeFirstResponder()
        }
        return textField
    }
    
    func updateUIView(_ uiView: UITextField, context: Context) {
        if didStartEditing {
            if let selectedDate = date {
                uiView.textColor = UIColor.black
                uiView.textual content = selectedDate.stringFromDate(with: "dd/MM/yyyy")
            }
        } else {
            uiView.textual content = placeholder
            uiView.textColor = UIColor.lightGray
        }
        if maxDate == nil {
            datePicker.maximumDate = bindableMaxDate?.dateByAddingDays(3)
            datePicker.reloadInputViews()
        }
    }
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    class Helper {
        public var onDateValueChanged: (() -> Void)?
        public var onDoneButtonTapped: (() -> Void)?
        public var onEditingStarted: (() -> Void)?
        @objc func dateValueChanged() {
            onDateValueChanged?()
        }
        @objc func doneButtonTapped() {
            onDoneButtonTapped?()
        }
        @objc func editingStarted() {
            onEditingStarted?()
        }
    }
    class Coordinator {}
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here