Swift - 文本字段中的字符不可见
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift - 文本字段中的字符不可见相关的知识,希望对你有一定的参考价值。
在我的应用程序中,我有几个定制设计的游戏来练习拼写和语法。整个过程一直很好,直到ios 11发布。
我的应用程序如何工作的概述:
- 游戏从我们的服务器接收一个随机的单词和图片,并自动创建一堆文本字段(在水平堆栈视图中)
- 文本字段的数量类似于接收字的字母数量(例如,“throw”一词将创建五个彼此相邻的文本字段)
- 该视图链接到自定义键盘,直到所有问题都得到正确回答才会显示
- 如果用户的输入与预期答案不相似,则文本字段会自行重置
iOS 11以来的问题
只有前两个问题不会在各自的文本字段中显示用户的输入(第一个字母似乎总是有效)。控制台还确认输入和字母的比较确实有效,但文本字段始终保持空白。游戏甚至完全重置,如果单词被正确回答,则转到下一个问题。奇怪的是问题3,4和5没有这个问题。
在下面的示例中,您可以看到发生了什么。第一个问题(“工厂”)仅显示第一个字母。第二个问题(“拍手”)做同样的事情。第三个问题(“咬”)没有这个问题。我的自定义光标工作正常并自动跳转到下一个框。
Swift代码(仅限功能)
let TEXT_FIELD_TAG = 1000
let CURSOR_INDEX = 0
var game: GameDictionary? {
didSet {
self.m_Client.GetMediaImage(game?.Image) { (image) in
self.imageView.image = image
}
self.selectedIndex = 0
self.answerChars = (game?.Content?.map { String($0).lowercased() })!
self.views = (0..<self.answerChars.count).map { _ in UITextField() }
var index: NSInteger = TEXT_FIELD_TAG
for textField in self.views {
textField.backgroundColor = .white
textField.textColor = Constants.MAIN_THEME_COLOR
textField.font = UIFont(name: Constants.GAME_FONT_TYPE_1, size: scaledFontSize(fontSize: 36))
textField.textAlignment = .center
textField.delegate = self
textField.layer.cornerRadius = 10
textField.autocapitalizationType = .none
textField.tag = index
textField.isUserInteractionEnabled = true
// Create the cursor object and animate the blinking
let cursor = UIView(frame: CGRect(x: scaledViewSize(viewSize: 10), y: scaledViewSize(viewSize: 10), width: 2, height: scaledViewSize(viewSize: 30)))
cursor.backgroundColor = Constants.MAIN_THEME_COLOR
cursor.isHidden = true
UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {() -> Void in
cursor.alpha = 0
}, completion: {(_ animated: Bool) -> Void in
cursor.alpha = 1
}
)
textField.addSubview(cursor)
self.container.addArrangedSubview(textField)
index+=1
}
}
}
// MARK: Question handlers
override func prepareForReuse() {
super.prepareForReuse()
inputArray.removeAll()
answerChars.removeAll()
self.selectedIndex = 0
for view in self.views {
view.removeFromSuperview()
}
self.views.removeAll()
}
override func onFocus() {
self.resetCursorTextField()
keyboardLauncher.onKeyPressed = { character in
self.views[self.selectedIndex].text = character
self.textChanged(sender: self.views[self.selectedIndex])
}
keyboardLauncher.showKeyboard()
keyboardLauncher.enableKeyboard()
}
func resetCursorTextField() {
for (index, textfield) in self.views.enumerated() {
textfield.subviews[CURSOR_INDEX].isHidden = (index == 0) ? false : true
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let newLength = text.count + string.count - range.length
return newLength <= 1
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
func resetInputViews() {
inputArray.removeAll()
self.selectedIndex = 0
self.resetCursorTextField()
for tag in TEXT_FIELD_TAG...(TEXT_FIELD_TAG + (self.answerChars.count) - 1) {
let textField = self.container.viewWithTag(tag) as! UITextField!
textField?.text = ""
if (tag == TEXT_FIELD_TAG) {
textField?.becomeFirstResponder()
}
}
}
func textChanged(sender: UITextField) {
if (sender.text?.count)! > 0 {
inputArray.append((sender.text?.lowercased())!)
if self.answerChars[self.selectedIndex] != inputArray.last! {
self.delegate?.updateAnswer(inputArray.joined(), false)
self.resetInputViews()
} else {
self.selectedIndex+=1
let nextField = sender.superview?.viewWithTag(sender.tag + 1) as! UITextField!
nextField?.becomeFirstResponder()
sender.subviews[CURSOR_INDEX].isHidden = true
nextField?.subviews[CURSOR_INDEX].isHidden = false
if self.answerChars.count == inputArray.count {
playCorrectAnswer()
isCompleted = true
keyboardLauncher.disableKeyboard()
self.textField?.isEnabled = false
self.delegate?.updateAnswer(inputArray.joined(), true)
self.delegate?.nextQuestion(self)
}
}
}
}
CellForItemAtIndexpath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == self.m_Collection?.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gameScoreOverviewCell, for: indexPath) as! GameScoreOverviewCell
cell.closeViewButton.addTarget(self, action: #selector(closeView(sender:)), for: .touchUpInside)
cell.rulesButton.addTarget(self, action: #selector(handleAlertScoreOverview(sender:)), for: .touchUpInside)
return cell
}
let game = self.m_Collection![indexPath.item]
if game.StarryGrade == StarryGrade.Grade4 && game.Level == Level.Level1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: fourthGradeCellLevel1, for: indexPath) as! GameCellFourthGradeLevel1
cell.cancelButton.addTarget(self, action: #selector(closeViewWithAlert(sender:)), for: .touchUpInside)
cell.questionLabel.text = "Question (indexPath.item + 1) of (self.m_Collection!.count)"
cell.game = game
cell.delegate = self
keyBoardIsVisible = true
return cell
}
}
答案
这可能不是一个程序问题,而是一个接口问题。请尝试以下方法:
- 选择“查看UI层次结构”。
- 在Xcode中解构UI时,单击包含隐藏字符的文本字段。
- 你现在应该检查几件事:
在右侧面板的Object Inspector中,检查以确保Text Field的
Title
属性是您键入的字符(如果没有,Text Field不捕获键盘输入) 同样在Object Inspector中,确保ViewVisibility
属性设置为Not Hidden 在“大小检查器”(也在右侧面板中)中,检查以确保“文本字段”足够大以显示该字符
我以前遇到过类似的问题,这些步骤让我可以确定问题的原因。我希望这有帮助。
以上是关于Swift - 文本字段中的字符不可见的主要内容,如果未能解决你的问题,请参考以下文章