ios – Easy methods to cancel faucet on UIDatePicker or dismiss it programatically?

0
1
ios – Easy methods to cancel faucet on UIDatePicker or dismiss it programatically?


You can’t cancel a faucet after it begins with .compact fashion UIDatePicker, however you’ll be able to:

  1. Forestall the faucet utilizing an overlay (UIButton) that intercepts contact.

  2. Or, use a UITextField + .inputView for higher management through delegate.

  3. Keep away from counting on .editingDidBegin or toggling isEnabled dynamically. It’s too late.

Really useful Answer: Use a Clear Overlay or Customized Wrapper

As a substitute of attempting to cancel the occasion after it begins, you’ll be able to block the interplay altogether until the password is verified.

Choice 1: Wrap the UIDatePicker in a Customized Container with a Clear Button Overlay

let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false

let datePicker = UIDatePicker()
datePicker.preferredDatePickerStyle = .compact
datePicker.datePickerMode = .time
datePicker.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(datePicker)

// Overlay button to dam interplay
let blockerButton = UIButton()
blockerButton.translatesAutoresizingMaskIntoConstraints = false
blockerButton.backgroundColor = .clear
blockerButton.addTarget(self, motion: #selector(promptForPassword), for: .touchUpInside)
containerView.addSubview(blockerButton)

// Constraints
NSLayoutConstraint.activate([
    datePicker.topAnchor.constraint(equalTo: containerView.topAnchor),
    datePicker.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
    datePicker.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
    datePicker.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),

    blockerButton.topAnchor.constraint(equalTo: containerView.topAnchor),
    blockerButton.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
    blockerButton.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
    blockerButton.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
])

Then:

@objc func promptForPassword() {
    // Ask for password right here
    // If legitimate, take away blocker
    if isPasswordCorrect {
        blockerButton.removeFromSuperview()
    } else {
        showPasswordError()
    }
}

Choice 2: Use a UITextField + UIDatePicker as inputView (Full Management)

let timeField = UITextField()
timeField.placeholder = "Choose Time"
timeField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(timeField)

let picker = UIDatePicker()
picker.datePickerMode = .time
picker.preferredDatePickerStyle = .wheels
picker.addTarget(self, motion: #selector(dateChanged(_:)), for: .valueChanged)
timeField.inputView = picker

// Password examine earlier than modifying
timeField.delegate = self

And conform to:

extension YourViewController: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if !isPasswordCorrect {
            showPasswordError()
            return false
        }
        return true
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here