Swift - 文本字段中的字符不可见

时间:2017-12-12 05:09:25

标签: swift uitextfield ios11

在我的应用程序中,我有几个定制设计的游戏来练习拼写和语法。整个过程一直很好,直到iOS 11发布。

我的应用程序如何运作的概述:

  • 游戏从我们的服务器接收一个随机的单词和图片,并自动创建一堆文本字段(在水平堆栈视图中)
  • 文本字段的数量类似于接收字词的字母数量(例如,“throw”字样会创建五个彼此相邻的文本字段)
  • 视图链接到自定义键盘,直到所有问题都得到正确回答才会显示
  • 如果用户的输入与预期答案不相似,则文本字段会自行重置

iOS 11以来的问题

只有前两个问题不会在各自的文本字段中显示用户的输入(第一个字母似乎总是有效)。控制台还确认输入和字母的比较确实有效,但文本字段始终保持空白。游戏甚至完全重置,如果单词被正确回答,则转到下一个问题。 奇怪的是问题3,4和5没有这个问题。

在下面的示例中,您可以看到发生了什么。第一个问题(“工厂”)仅显示第一个字母。第二个问题(“拍手”)做同样的事情。第三个问题(“咬”)没有这个问题。我的自定义光标工作正常并自动跳转到下一个框。

The first three questions

Swift Code(仅限功能)

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  
    } 
}

1 个答案:

答案 0 :(得分:1)

这可能不是程序问题,而是界面问题。请尝试以下方法:

  1. 确保您正在编译Debug

  2. 的应用程序
  3. 重新创建左下图中的方案,您在其中键入了一些字母并将其隐藏: enter image description here

  4. 单击Debug Navigator中的此按钮:

  5. enter image description here

    1. 选择查看UI层次结构
    2. 在Xcode中解构您的UI时,单击包含隐藏字符的文本字段。
    3. 您现在应该检查以下几点:

      • 在右侧面板的Object Inspector中,检查以确保文本字段的Title属性是您键入的字符(如果没有,文本字段不捕获键盘输入)
      • 同样在Object Inspector中,确保将View Visibility属性设置为 Not Hidden
      • 在“大小”检查器中(也在右侧面板中),检查以确保“文本字段”足够大以显示该字符
    4. 之前我遇到过类似的问题,这些步骤让我可以确定问题的原因。我希望这会有所帮助。