如何在搜索栏中更改键盘语言?迅速

时间:2019-01-21 11:41:15

标签: swift xcode textfield searchbar

我如何更改TextField中的语言:

class langField: UITextField {
    var languageCode:String? {
        didSet {
            if self.isFirstResponder {
                self.resignFirstResponder()
                self.becomeFirstResponder()
            }
        }
    }

    override var textInputMode: UITextInputMode? {
        if let language_code = self.languageCode {
            for keyboard in UITextInputMode.activeInputModes {
                if let language = keyboard.primaryLanguage {
                    let locale = Locale.init(identifier: language)
                    if locale.languageCode == language_code {
                        return keyboard
                    }
                }
            }
        }
        return super.textInputMode
    }
}

然后:

let textFiled = langField() textField.languageCode =“ en”

并且当用户点击您的TextFiled语言时,语言将为英语

如何对搜索栏执行同样的操作?

我这样做:

extension UISearchBar {

    func textField() -> UITextField? {

        for view in subviews {

            for subview in view.subviews {

                if let textField = subview as? UITextField {
                    return textField
                }
            }
        }
        return nil
    }
}

但是当我调用textField时,没有.languageCode方法

0 个答案:

没有答案