我可以通过参考iPhone: Change Keyboard language programmatically将键盘语言从ja-JP更改为vi-VN。 但是我无法在同一代码上将vi-VN更改为en-US。
我不知道为什么会这样,但我已解决。解决方法是我总是在更改其他语言之前设置语言ja-JP(请参阅my project)。
此项目重现了问题。但是启用func selectLanguage中的BEGIN:至END:块将解决此问题。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var lang = "en-US"
@IBAction func selectLanguage(_ sender: UISegmentedControl) {
// BEGIN: Changing Keyboard language en-US to vi-VN is working if this comment this block out.
// lang = "ja-JP"
// self.textField.resignFirstResponder()
// self.textField.becomeFirstResponder()
// END: Changing Keyboard language en-US to vi-VN is working if this comment this block out.
switch sender.selectedSegmentIndex {
case 0: // English
lang = "en-US"
case 1: // Vietnamese
lang = "vi-VN"
case 2: // Japanese
lang = "ja-JP"
default:
break
}
// ReDisplay Keyboard
self.textField.resignFirstResponder()
self.textField.becomeFirstResponder()
}
override var textInputMode: UITextInputMode?{
var tim = super.textInputMode
for mode in UITextInputMode.activeInputModes {
if mode.primaryLanguage!.localizedStandardContains(lang) {
tim = mode
}
print("Available Keyboard: ", mode.primaryLanguage ?? "nil")
}
print("Selected language: ", tim?.primaryLanguage ?? "unknown")
return tim
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
我想知道解决此问题的正确方法。 谢谢。