UIDatePicker没有完全弹出在UITextField点击



我正在编写的应用程序是一个基于事件的应用程序。在屏幕上,你将创建一个新的事件,我有一个UITextField与UIDatePicker设置作为其输入视图初始化为:

lazy var eventDateTxt: UITextField = {
let tf = UITextField()
tf.attributedPlaceholder = NSAttributedString(string: "Pick Your Event Date",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.zipVeryLightGray])
tf.font = .zipBody
tf.borderStyle = .roundedRect

tf.tintColor = .white
tf.backgroundColor = .zipGray
tf.textColor = .white
tf.adjustsFontSizeToFitWidth = true
tf.minimumFontSize = 10.0;

let datePicker = UIDatePicker()
datePicker.datePickerMode = .dateAndTime
datePicker.minimumDate = Date()
tf.inputView = datePicker

datePicker.addTarget(self, action: #selector(dateChanged), for: .valueChanged)
return tf
}()

datechange是这样的

@objc func dateChanged(sender: UIDatePicker){
let dateFormat = DateFormatter()
dateFormat.dateStyle = .long
dateFormat.timeStyle = .short
eventDateTxt.text = dateFormat.string(from: sender.date)

event.startTime = sender.date
}

,虽然这并不重要,这里是我的代码UITextFieldDelegate

extension NewEventViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == titleText {
if textField.text == "Event Title" {
textField.text = ""
}
}
}

func textFieldDidEndEditing(_ textField: UITextField) {
if textField == titleText {
if textField.text == "" {
textField.text = "Event Title"
}
}
}
}

现在的问题是:当我点击UITextField打开DatePicker时,它会在屏幕底部弹出一个小的弹出视图,看起来像这样:

https://ibb.co/WBJ9GsN

注意图片的最底部

现在如果你点击底部的日期它会像预期的那样打开日期选择器,看起来像这样:

https://ibb.co/N7VvfX6

执行此操作时,console

中将出现错误。
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002d79ea0 'UIView-bottom-readableContentGuide-constraint' UILayoutGuide:0x60000379d340'UIViewLayoutMarginsGuide'.bottom == UILayoutGuide:0x60000379d260'UIViewReadableContentGuide'.bottom   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2021-08-11 12:52:37.262846-0400 zip_official[93261:10730049] [DatePicker] UIDatePicker 0x7faa2732fe80 is being laid out below its minimum width of 280. This may not look like expected, especially with larger than normal font sizes.

使用建议的断点没有导致任何结果。

所以我的问题是:我如何使它,所以日期picker立即弹出没有小底部标签。

这个日期选择器的事情很麻烦。在前两个iOS版本中,它改变了两次。我之前也遇到过同样的问题,我所做的是:

使日期选择器贴在底部(用一些常数将其放置在安全区域上方)。在你的情况下,我会给它一个框架。

let datePicker = UIDatePicker(frame: CGRect(x: eventDateTxt.frame.minX, y: eventDateTxt.frame.minY, width: view.frame.width, height: 150))

一些伪代码(用于iOS 14或更高版本):

let datePicker = UIDatePicker()
datePicker.preferredDatePickerStyle = .wheels

如果你的iOS版本<14,那么也许可以用框架。

为了更好的UI输出

if #available(iOS 14.0, *) {
datePicker.preferredDatePickerStyle = .inline
}  else if #available(iOS 13.4, *) {
datePicker.preferredDatePickerStyle = .wheels
}