Open UIDatePicker programmatically in iOS 14

后端 未结 9 1497
南笙
南笙 2020-12-23 16:46

I have this normal UIDatePicker (sorry that it\'s not in English, please ignore that):

\"my

相关标签:
9条回答
  • 2020-12-23 17:31

    Try this -

    datePicker.preferredDatePickerStyle = .inline
    
    0 讨论(0)
  • 2020-12-23 17:32

    It's impossible to open a UIDatePicker with UIKit14 from within the code

    • UIDatePicker has no API for doing this
    • UIKit doesn't allow us to create a UIEvents or UITouch object which has any impact on UIKit.
    0 讨论(0)
  • 2020-12-23 17:33

    Unfortunately it's not possible to programatically display the date selector popup, at least not resorting to hacks that might result in your app being rejected:

    • the UIDatePicker control doesn't expose any events that can be programatically triggered via sendActions(for:) - allControlEvents returns an empty option set

    • programatically simulating a user click is officially impossible on iOS - there is no public API to achieve this, all workarounds imply resorting to private API's

    • if the above wouldn't be enough, the UI structure of UIDatePicker is composed of private classes:

    The conclusion is that in its current state, the UIDatePicker UI can be controlled only via user interactions. Maybe this will change in the future, for example UIButton's expose actions for triggering the same behaviour as when the user tapped the button, so maybe UIDatePicker will expose such actions in the future.

    0 讨论(0)
  • 2020-12-23 17:33

    Define these variables

    var datePicker: UIDatePicker!
    var datePickerConstraints = [NSLayoutConstraint]()
    var blurEffectView: UIView!
    

    Then create a function showDatePicker() that you call to trigger the display of the UIDatePicker. First, create a subview with the same size of his parent View and give it a blur effect. Then add the UIDatePicker, as a subview on top of the blurred view.

       func showDatePicker() {
            datePicker = UIDatePicker()
            datePicker?.date = Date()
            datePicker?.locale = .current
            datePicker?.preferredDatePickerStyle = .inline
            datePicker?.addTarget(self, action: #selector(dateSet), for: .valueChanged)
            addDatePickerToSubview()
        }
    
        func addDatePickerToSubview() {
            guard let datePicker = datePicker else { return }
            // Give the background Blur Effect
            let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.regular)
            blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = self.view.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.view.addSubview(blurEffectView)
            self.view.addSubview(datePicker)
            datePicker.translatesAutoresizingMaskIntoConstraints = false
            centerDatePicker()
            view.bringSubviewToFront(datePicker)
        }
    
        func centerDatePicker() {
            guard let datePicker = datePicker else { return }
            // Center the Date Picker
            datePickerConstraints.append(datePicker.centerYAnchor.constraint(equalTo: self.view.centerYAnchor))
            datePickerConstraints.append(datePicker.centerXAnchor.constraint(equalTo: self.view.centerXAnchor))
            NSLayoutConstraint.activate(datePickerConstraints)
        }
    

    And finally, once a date a selected in the UIDatePicker

     @objc func dateSet() {
            // Get the date from the Date Picker and put it in a Text Field
            compactDatePicker.date = datePicker.date
            blurEffectView.removeFromSuperview()
            datePicker.removeFromSuperview()
        }
    

    Not entirely sure about the blurr effect so hopefully someone can build on this solution.

    0 讨论(0)
  • 2020-12-23 17:35

    I guess you are using the compact date picker style which is a new feature on iOS 14. And I think you can simply use ‏‏‏‏the inline style instead of compact to show the full calendar UI. And don't forget that the UIDatePicker is just an UIView so you can customize your presentation to implement the blur effect you want.‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏

    0 讨论(0)
  • 2020-12-23 17:44

    I have the same problem. Im using following hack:

    let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    datePicker.preferredDatePickerStyle = .compact
    
    // Removes Labels
    datePicker.subviews.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
    

    Then I simply add this date picker on top of a view, which should open this popup.

    0 讨论(0)
提交回复
热议问题