Skip to content

Instantly share code, notes, and snippets.

@IamAlchemist
Created October 10, 2016 03:07
Show Gist options
  • Save IamAlchemist/c582deb518dfa0535157e1d1f9fe6115 to your computer and use it in GitHub Desktop.
Save IamAlchemist/c582deb518dfa0535157e1d1f9fe6115 to your computer and use it in GitHub Desktop.
a date picker in alert view
@IBAction func selectDate(_ sender: UIButton) {
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
let alert = UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: .actionSheet)
alert.view.addSubview(datePicker)
datePicker.snp.makeConstraints { (make) in
make.centerX.equalTo(alert.view)
make.top.equalTo(alert.view).offset(8)
}
let ok = UIAlertAction(title: "确定", style: .default) { (action) in
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.string(from: datePicker.date)
print(dateString)
}
let cancel = UIAlertAction(title: "取消", style: .default, handler: nil)
alert.addAction(ok)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
@FitzAfful
Copy link

Saved a life 5 years later. :)

@soberman
Copy link

soberman commented Apr 3, 2023

Now that's an "out of the box" thinking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment