【问题标题】:Detect keyboard type changes in custom keyboard for iOS 8检测 iOS 8 自定义键盘中的键盘类型更改
【发布时间】:2014-11-24 03:37:24
【问题描述】:
我目前正在创建一个 iOS 8 自定义键盘扩展,并且我正在寻找一种方法来确定用户何时切换到另一个输入,以便更改我的布局。
例如,当用户选择UITextField 类型为UIKeyboardTypeEmailAddress 时,我打算呈现一个自定义键盘,而当用户选择另一个UITextField 类型为UIKeyboardTypeDecimalPad 时,我想注意到它并更新我的键盘布局。当键盘类型发生变化以更新键盘布局时,如何获得通知?
【问题讨论】:
标签:
objective-c
ios8
ios-app-extension
custom-keyboard
【解决方案1】:
您可以在textDidChange 中检测到键盘类型的更改。您需要获取UITextDocumentProxy,然后检查代理的keyboardType。如果它是您想要支持的键盘类型,那么您可以呈现相应的 UI。例如,您将通过以下方式检测何时应显示电子邮件键盘:
override func textDidChange(textInput: UITextInput) {
// Called when the document context is changed - theme or keyboard type changes
var proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardType == UIKeyboardType.EmailAddress {
//add code here to display email input keyboard
}
}