有没有办法禁用UITextField的听写支持?

时间:2016-05-26 16:35:59

标签: xcode swift tvos siri-remote

所以我正在使用swift的tvos应用程序,我想知道是否可以禁用自定义UITextField的听写支持。它并没有真正起作用,我也不希望用户能够这样做

2 个答案:

答案 0 :(得分:0)

您是否尝试使用textfield的keyboardType属性?也许您可以更改文本输入类型,因此自动不显示听写功能。

文档:https://developer.apple.com/library/tvos/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/occ/intfp/UITextInputTraits/keyboardType

答案 1 :(得分:0)

这是基于@BadPirate's hack的Swift 4解决方案。它会触发初始铃声,说明听写已开始,但听写布局将永远不会出现在键盘上。

这不会从键盘上隐藏听写按钮:为此,唯一的选择似乎是使用带有UIKeyboardType.emailAddress的电子邮件布局。


在拥有要禁用听写功能的viewDidLoad的视图控制器的UITextField中:

// Track if the keyboard mode changed to discard dictation
NotificationCenter.default.addObserver(self,
                                       selector: #selector(keyboardModeChanged),
                                       name: UITextInputMode.currentInputModeDidChangeNotification,
                                       object: nil)

然后自定义回调:

@objc func keyboardModeChanged(notification: Notification) {
    // Could use `Selector("identifier")` instead for idSelector but
    // it would trigger a warning advising to use #selector instead
    let idSelector = #selector(getter: UILayoutGuide.identifier)

    // Check if the text input mode is dictation
    guard
        let textField = yourTextField as? UITextField
        let mode = textField.textInputMode,
        mode.responds(to: idSelector),
        let id = mode.perform(idSelector)?.takeUnretainedValue() as? String,
        id.contains("dictation") else {
            return
    }

    // If the keyboard is in dictation mode, hide
    // then show the keyboard without animations
    // to display the initial generic keyboard
    UIView.setAnimationsEnabled(false)
    textField.resignFirstResponder()
    textField.becomeFirstResponder()
    UIView.setAnimationsEnabled(true)

    // Do additional update here to inform your
    // user that dictation is disabled
}