iOS Swift UIDatePicker内联样式时间和日期标题在inputView上设置时不显示

lx0bsm1f  于 2023-06-25  发布在  iOS
关注(0)|答案(2)|浏览(165)

我使用inlineUIDatePickerdateAndTime模式作为TextFieldinputView。但是,它不显示时间和日期标题。就像下面的图片。

下面是我的代码集UIDatePicker:-

func setupDatePicker() {
        datePicker = UIDatePicker()
        datePicker?.date = Date()
        datePicker?.locale = .current
        datePicker?.minimumDate = Date()
        datePicker?.datePickerMode = .dateAndTime
        if #available(iOS 14.0, *) {
            datePicker?.preferredDatePickerStyle = .inline
        }
        datePicker?.addTarget(self, action: #selector(handleDateSelection), for: .valueChanged)
        
        txtDate.inputView = datePicker
    }

请帮助我如何显示时间和日期标题在这个像下面。


]
先谢谢你了。

xuo3flqw

xuo3flqw1#

有一个解决方法。这个想法是将UIDatePicker Package 在另一个UIView中,该框架提供足够的空间来显示整个UIDatePicker。然后使用UIDatePicker的自动布局将边缘捕捉到超级视图,或者只是更新框架以匹配超级视图。
代码片段:

let picker = UIDatePicker()

// Your additional code to set up `UIDatePicker`

if #available(iOS 14.0, *) {
    picker.preferredDatePickerStyle = .inline
}

// Big enough frame
let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 420)
let pickerWrapperView = UIView(frame: rect)
pickerWrapperView.addSubview(picker)

// Adding constraints
picker.translatesAutoresizingMaskIntoConstraints = false
picker.leadingAnchor.constraint(equalTo: pickerWrapperView.leadingAnchor).isActive = true
picker.trailingAnchor.constraint(equalTo: pickerWrapperView.trailingAnchor).isActive = true
picker.topAnchor.constraint(equalTo: pickerWrapperView.topAnchor).isActive = true
picker.bottomAnchor.constraint(equalTo: pickerWrapperView.bottomAnchor).isActive = true

// Using wrapper view instead of picker
textField.inputView = pickerWrapperView
yhived7q

yhived7q2#

基于上一个答案https://stackoverflow.com/a/72329947/4121889

func setupDatePicker() {
     let datePic = UIDatePicker()
     datePic.datePickerMode = .dateAndTime
     datePic.preferredDatePickerStyle = .inline
     datePic.addTarget(self, action: #selector(dateTimeChanged), for: .valueChanged)
     let extraHeight = (UIScreen.main.bounds.height - datePickerView.systemLayoutSizeFitting(self.bounds.size).height) / 4
     let embededView = datePic.embedToView(rect: CGRect(x: 0, y: 0, width: datePic.systemLayoutSizeFitting(view.bounds.size).width, height: 
     datePic.systemLayoutSizeFitting(view.bounds.size).height + extraHeight))//just setting 440 as height i think is fine but leaves bigger bottom space on older models
     timeTextField.inputView = embededView
   }

   extension UIDatePicker {
    func embedToView(rect: CGRect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 440)) -> UIView {
     let pickerWrapperView = UIView(frame: rect)
     pickerWrapperView.addSubview(self)
     self.translatesAutoresizingMaskIntoConstraints = false
     self.leadingAnchor.constraint(equalTo: pickerWrapperView.leadingAnchor).isActive = true
     self.trailingAnchor.constraint(equalTo: pickerWrapperView.trailingAnchor).isActive = true
     self.topAnchor.constraint(equalTo: pickerWrapperView.topAnchor).isActive = true
     self.bottomAnchor.constraint(equalTo: pickerWrapperView.bottomAnchor).isActive = true
     return pickerWrapperView
  }
 }

相关问题