gpt4 book ai didi

ios - 键盘超越日期选择器

转载 作者:行者123 更新时间:2023-11-30 12:25:58 26 4
gpt4 key购买 nike

当您点击文本字段时,我正在尝试显示日期选择器。这一切似乎工作正常,但一旦我关闭日期选择器,它就会显示键盘。

所以我基本上必须将其关闭两次才能真正消失。

我为选择器创建了一个文本字段类,如下所示

编辑将所有代码移至文本字段类

protocol TextFieldPickerDelegate {
func pickerValueDidChange(date: Date, pickerMode: UIDatePickerMode, sender: TextFieldPicker)
}

public class TextFieldPicker: UITextField, UITextFieldDelegate {

var pickerMode: UIDatePickerMode = .date

@IBInspectable var pickerType: String? {
willSet {
for s in (newValue?.lowercased().getStrings())! {
switch s {
case "date":
pickerMode = .date
case "dateandtime":
pickerMode = .dateAndTime
case "countdowntimer":
pickerMode = .countDownTimer
case "time":
pickerMode = .time
default:
pickerMode = .date
}
}
}
didSet {
setupTextField()
}
}

var textFieldPickerDelegate: TextFieldPickerDelegate?

func setupTextField() {
createPicker()
}

override public func draw(_ rect: CGRect) {
super.draw(rect)

delegate = self
}

override public func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}

override public func shouldChangeText(in range: UITextRange, replacementText text: String) -> Bool {
return false
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return false
}

public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.resignFirstResponder()
return true
}

public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
self.resignFirstResponder()
return true
}

func createPicker() {
let datePicker = UIDatePicker()
datePicker.datePickerMode = pickerMode
datePicker.addTarget(self, action: #selector(pickerValueChange(_:)), for: .valueChanged)

let pickerToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
pickerToolBar.tintColor = UIColor.gray
let doneButton = UIBarButtonItem(title: NSLocalizedString("done", comment: ""), style: .done, target: self, action: #selector(closePicker(_:)))
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
pickerToolBar.setItems([space, doneButton], animated: true)

self.inputView = datePicker
self.inputAccessoryView = pickerToolBar
}

func closePicker(_ sender: Any) {
self.resignFirstResponder()
}

func pickerValueChange(_ sender: UIDatePicker) {
self.textFieldPickerDelegate?.pickerValueDidChange(date: sender.date, pickerMode: sender.datePickerMode, sender: self)
}

public func getDateString(date: Date, format: String) -> String {
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = format
return timeFormatter.string(from: date)
}
}






// =================================================== OptionSet =================================================== \\
public struct PickerModes: OptionSet {
private enum PickerMode: Int, CustomStringConvertible {
case Date=1
case DateAndTime=2
case CountDownTimer=4
case Time=8

public var description: String {
var shift = 0
while (rawValue.hashValue >> shift != 1) { shift += 1 }
return ["date", "dateandtime", "countdowntimer", "time"][shift]
}
}
public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ mode: PickerMode) { self.rawValue = mode.rawValue }

static let Date = PickerModes(PickerMode.Date)
static let DateAndTime = PickerModes(PickerMode.DateAndTime)
static let CountDownTimer = PickerModes(PickerMode.CountDownTimer)
static let Time = PickerModes(PickerMode.Time)
}

extension String {
func getStrings() -> [String] {
var stringArray: [String] = []
let strings = self.characters.split{$0 == ","}.map(String.init)
for s in strings {
let string = s.removeSpaces()
stringArray.append(string)
}
return stringArray
}

func removeSpaces() -> String {
if self.characters.first == " " {
var copy = self
copy.characters.removeFirst()
return copy.removeSpaces()
} else {
return self
}
}
}

此类完全负责显示日期选择器并处理显示正确类型的日期选择器,以防一个屏幕中需要多个不同的选择器。

编辑2
如果其他人尝试我的代码,它将正常工作,没有任何问题。我遇到的问题是,我不小心将自定义类设置为包含我的 UITextFieldUIView,因此当我点击文本字段时,它会首先触发 的键盘>UIView 然后是 UIDatePicker,因此当 UIDatePicker 退出时键盘仍会在那里。

最佳答案

我一直在做一些研究,在我看来,在文本字段中创建日期选择器有更简单的方法。你可以只使用其中之一。 https://blog.apoorvmote.com/change-textfield-input-to-datepicker/

关于ios - 键盘超越日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44217849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com