1

我有一个 UILabel 子类,符合 UIKeyInputs 协议。但是当显示键盘时,预测视图位于键盘键下方:这是屏幕截图:

截图

这是实现:

class CustomLabel: UILabel {
    // some implementation, nothing important to effect keyboard.
}

extension CustomLabel: UIKeyInput {

  var hasText: Bool {
    if let text = self.text {
      return text.isEmpty
    }
    return false
  }

  func insertText(_ text: String) {
    self.text?.append(text)
  }

  func deleteBackward() {
    guard let text = self.text else { return }
    self.text = String(text.dropLast())
  }

  override var canBecomeFirstResponder: Bool {
    return true
  }

  private var autocapitalizationType: UITextAutocapitalizationType {
    return .none
  }

  private var autocorrectionType: UITextAutocorrectionType {
    return .no
  }

  private var spellCheckingType: UITextSpellCheckingType {
    return .no
  }

  private var keyboardType: UIKeyboardType {
    return .twitter
  }

  private var returnKeyType: UIReturnKeyType {
    return .done
  }

  private var keyboardAppearance: UIKeyboardAppearance {
    return .default
  }

}

我不明白的一件事是 autocapitalizationType、autocorrectionType 和其他用于键盘外观的协议方法没有被调用。

我希望有人能帮帮忙。谢谢你。

4

1 回答 1

0

因为Swift4你必须使用UITextInputTraitsas

@objc var keyboardType: UIKeyboardType = .default
@objc var autocorrectionType: UITextAutocorrectionType = .no

或者

@objc  var keyboardAppearance: UIKeyboardAppearance {
    get { return .dark }
    set(value) {}
}
于 2018-03-29T09:19:46.257 回答