Vij*_*jay 4 datepicker uiactionsheet swift
有没有办法从UIAlertControllerStyle.ActionSheet呈现日期选择器?有办法在ActionSheet中添加按钮并向这些按钮添加操作,但我找不到在ActionSheet中添加datepicker或任何自定义视图的解决方案.
kar*_*agu 11
这是不可能的,但是当用户单击文本字段时,您可以使用UItextfield输入视图和附件视图来显示和关闭日期选择器
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
}
}
Run Code Online (Sandbox Code Playgroud)
是的,绝对可以在UIAlertController中显示UIDatePicker。在Swift 4中查看以下代码。我在Projects中多次使用了此代码,并且效果很好。
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6384 次 |
| 最近记录: |