【问题标题】:UIDatePicker not fully popping up on UITextField clickUIDatePicker 没有在 UITextField 点击时完全弹出
【发布时间】:2021-08-11 16:55:39
【问题描述】:

我正在编写的应用程序是基于事件的应用程序。在您将创建一个新事件的屏幕上,我有一个 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
}()

dateChanged 看起来像这样

@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

注意图像的最底部。

现在,如果您单击底部的日期,它会按预期打开 DatePicker,如下所示:

https://ibb.co/N7VvfX6

这样做时,控制台中会出现错误

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.

使用建议的断点并没有把我带到任何地方。

所以我的问题是:我怎样才能让 DatePicker 立即弹出而没有底部的小标签。

【问题讨论】:

    标签: ios swift iphone uidatepicker


    【解决方案1】:

    这个日期选择器的事情很让人头疼。最近两个 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 版本

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2013-09-23
      • 2019-02-28
      • 1970-01-01
      相关资源
      最近更新 更多