DatePicker & Alerts
DatePicker & Alert
Advanced usage of native UIAlertController with DatePicker.
Features
- Custom pickers based on UIDatePicker.
- Example using a Storyboard.
- Easy contentViewController placement.
- Attributed title label and message label.
- Button customization: image and title color.
- Understandable action button placement.
- Easy presentation.
- Pure Swift 4
- New Alert
- Set and styling title
- Set and styling message
- Add button with image
- Show Alert
let alert = UIAlertController(style: .alert, title: "Title", message: "Message")
// or
let alert = UIAlertController(style: .alert)
alert.set(title: "Title", font: .systemFont(ofSize: 20), color: .black)
// or
alert.setTitle(font: .systemFont(ofSize: 20), color: .black)
alert.set(message: "Message", font: .systemFont(ofSize: 16), color: .black)
// or
alert.setMessage(font: .systemFont(ofSize: 16), color: .black)
alert.addAction(image: image, title: "Title", color: .black, style: .default) { action in
// completion handler
}
// show alert
alert.show()
// or show alert with options
alert.show(animated: true, vibrate: true) {
// completion handler
}
Pickers
For UX better to use
.actionSheet
style in UIAlertController
when set picker into contentViewController
. If you like you can use .alert
style as well, buy .actionSheet
style is wider and User can see more as well as action button is placing at bottom that also more convenience for User to touch it.
UITextField In native UIAlertController you can only add
UITextField
to .alert
style with default style and you can not change such properties as .borderColor
, .borderWidth
, .frame.size
and so on. But if you make your own UIViewController
with UITextField
, it will solve all these problems.DatePicker
UIDatePicker
does not look very much in .alert
style.@IBAction func onClickEvent(_ sender: UIButton) {let alert = UIAlertController(title: "Date", message: nil, preferredStyle: .actionSheet)alert.addDatePicker(mode: .dateAndTime, date: Date(), minimumDate: nil , maximumDate: nil) { date inlet dateFormatter = DateFormatter()dateFormatter.dateFormat = "dd - MMMM - YYYY"sender.setTitle(dateFormatter.string(from: date), for: .normal)}alert.addAction(title: "OK", style: .cancel)alert.show()}
- Patel Poorav
Comments
Post a Comment