在 Swift 的文本输入中输入每个字符时键盘关闭
Keyboard dismisses on every character typed in text input in Swift
我有一个 textField。
private var verseTitle: UITextField = {
let tf = UITextField()
tf.placeholder = "TITLE"
tf.font = UIFont(suite16: .tBlackItalic, size: 18)
tf.textColor = .black.withAlphaComponent(0.5)
tf.returnKeyType = .done
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
在 viewDidLoad 方法中,我已将自己指定为委托。
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
verseTitle.delegate = self
}
在 viewDidLayout 方法中,我使用堆栈视图将 textField 添加到视图。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//Title
let titleStack = UIStackView()
titleStack.axis = .horizontal
titleStack.alignment = .center
titleStack.distribution = .equalSpacing
titleStack.spacing = 8
titleStack.addArrangedSubview(verseTitle)
titleStack.addArrangedSubview(floorView)
titleStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleStack)
NSLayoutConstraint.activate([
floorView.heightAnchor.constraint(equalToConstant: 13),
floorView.widthAnchor.constraint(equalToConstant: 13),
titleStack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
])
}
现在,我面临的问题是,当我开始在 textField 中键入内容时,只有在我键入一个字母后键盘才会消失。我不确定为什么会这样。输入每个字母后,我必须点击该字段。出于某种原因,输入每个字母后焦点会从字段中移开(除非我点击建议的自动更正 - 整个字符串会立即正确添加到字符串中)
这是您的视图,包括 UITextField
在每次击键后得到 re-created,因为您的视图构造位于:
viewDidLayoutSubviews()
, When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method.
要修复它,将代码移动到 ViewDidLoad
,这样视图只创建一次:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
verseTitle.delegate = self
let titleStack = UIStackView()
titleStack.axis = .horizontal
titleStack.alignment = .center
titleStack.distribution = .equalSpacing
titleStack.spacing = 8
titleStack.addArrangedSubview(verseTitle)
titleStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleStack)
NSLayoutConstraint.activate([
titleStack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
])
}
我有一个 textField。
private var verseTitle: UITextField = {
let tf = UITextField()
tf.placeholder = "TITLE"
tf.font = UIFont(suite16: .tBlackItalic, size: 18)
tf.textColor = .black.withAlphaComponent(0.5)
tf.returnKeyType = .done
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
在 viewDidLoad 方法中,我已将自己指定为委托。
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
verseTitle.delegate = self
}
在 viewDidLayout 方法中,我使用堆栈视图将 textField 添加到视图。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//Title
let titleStack = UIStackView()
titleStack.axis = .horizontal
titleStack.alignment = .center
titleStack.distribution = .equalSpacing
titleStack.spacing = 8
titleStack.addArrangedSubview(verseTitle)
titleStack.addArrangedSubview(floorView)
titleStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleStack)
NSLayoutConstraint.activate([
floorView.heightAnchor.constraint(equalToConstant: 13),
floorView.widthAnchor.constraint(equalToConstant: 13),
titleStack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
])
}
现在,我面临的问题是,当我开始在 textField 中键入内容时,只有在我键入一个字母后键盘才会消失。我不确定为什么会这样。输入每个字母后,我必须点击该字段。出于某种原因,输入每个字母后焦点会从字段中移开(除非我点击建议的自动更正 - 整个字符串会立即正确添加到字符串中)
这是您的视图,包括 UITextField
在每次击键后得到 re-created,因为您的视图构造位于:viewDidLayoutSubviews()
, When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method.
要修复它,将代码移动到 ViewDidLoad
,这样视图只创建一次:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
verseTitle.delegate = self
let titleStack = UIStackView()
titleStack.axis = .horizontal
titleStack.alignment = .center
titleStack.distribution = .equalSpacing
titleStack.spacing = 8
titleStack.addArrangedSubview(verseTitle)
titleStack.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(titleStack)
NSLayoutConstraint.activate([
titleStack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
])
}