Is there any way to present the datepicker from UIAlertControllerStyle.ActionSheet ? There is way to add buttons and add actions to those buttons in the ActionSheet, but i couldn't find a solution to add a datepicker or any custom view in the ActionSheet.
Asked
Active
Viewed 1.1k times
2 Answers
13
It is not possible, but you can use a UItextfield input view and accessory view to show and dismiss a datepicker when the user clicks the textfield
class KPDatePickerViewController: UIViewController {
var datePicker:UIDatePicker!
@IBOutlet var dateTextField:UITextField!
override func viewDidLoad() {
var customView:UIView = UIView (frame: CGRectMake(0, 100, 320, 160))
customView.backgroundColor = UIColor.brownColor()
datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
customView .addSubview(datePicker)
dateTextField.inputView = customView
var doneButton:UIButton = UIButton (frame: CGRectMake(100, 100, 100, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor .blueColor()
dateTextField.inputAccessoryView = doneButton
}
func datePickerSelected() {
dateTextField.text = datePicker.date.description
}
}

sf89
- 5,088
- 7
- 24
- 27

karthikPrabhu Alagu
- 3,371
- 1
- 21
- 25
-
Thank you karthik i tried the same thing but dunno how to dismiss this view! – Vijay Sep 16 '14 at 05:56
-
I used resignFirstResponder() to dismiss it and it worked well :) – Vijay Sep 16 '14 at 06:28
8
Yes it is definitely possible to present UIDatePicker in UIAlertController. Check out the following code in Swift 4. I have used this code many a times in Projects and it worked fine.
let dateChooserAlert = UIAlertController(title: "Choose date...", message: nil, preferredStyle: .actionSheet)
dateChooserAlert.view.addSubview(datePicker)
dateChooserAlert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: { action in
// Your actions here if "Done" clicked...
}))
let height: NSLayoutConstraint = NSLayoutConstraint(item: dateChooserAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.1, constant: 300)
dateChooserAlert.view.addConstraint(height)
self.present(dateChooserAlert, animated: true, completion: nil)

Hardik Trivedi
- 613
- 7
- 10
-
This works, with a caveat. Because you specify `style: .cancel` for the `UIAlertAction`, that same handler will fire when the user taps outside the bounds of the alert to dismiss it. You can use `style: .default`, but then you lose the ability to tap outside to dismiss. I ended up having two actions, a "Done" with `style: .default`, and a "Cancel" with `style: .cancel`. The resulting sheet UI is not as nice, but it was my only option. – marcshilling Jan 08 '20 at 22:19
-
1Could add to keep sizes confined to the alert sheet `datePicker.translatesAutoresizingMaskIntoConstraints = false datePicker.topAnchor.constraint(equalTo: dateChooserAlert.view.topAnchor, constant: 15).isActive = true datePicker.bottomAnchor.constraint(equalTo: dateChooserAlert.view.bottomAnchor, constant: -15).isActive = true datePicker.leadingAnchor.constraint(equalTo: dateChooserAlert.view.leadingAnchor, constant: 0).isActive = true datePicker.trailingAnchor.constraint(equalTo: dateChooserAlert.view.trailingAnchor, constant: 0).isActive = true` – Poh Peng Ric Apr 17 '20 at 09:30