键盘不显示带有 UITextFields 的语言切换按钮(地球仪)

Keyboard does not show language switch button (globe) with UITextFields

我在一个 UIViewController 中有 2 个 UITextField。

第一个是电子邮件。当它获得焦点时,键盘将显示语言切换按钮(地球)。

第二个是昵称。当它获得焦点时,键盘将显示没有地球按钮。

这两个UITextField的代码几乎是一样的。即使我将昵称 UITextField 更改为键盘类型作为电子邮件 UITextField,它也不会显示地球按钮。我不知道为什么第二个 UITextField 总是不显示地球按钮。

代码如下:

        mEmail = UITextField(frame: CGRect(x: PADDING,
                                           y: innerY,
                                           width: mInputArea!.bounds.width - PADDING*2,
                                           height: 40))
        mEmail!.textContentType = .emailAddress
        mEmail!.autocapitalizationType = .none
        mEmail!.keyboardType = .emailAddress
        mEmail!.layer.borderColor = ColorDef.background.cgColor
        mEmail!.layer.borderWidth = 1
        mEmail!.layer.cornerRadius = 4
        mEmail!.placeholder = localizedInfoPlist("Account_Email")
        mEmail!.clearButtonMode = .whileEditing
        mEmail!.returnKeyType = .next
        mEmail!.delegate = self
        mInputArea!.addSubview(mEmail!)
        innerY += (PADDING + mEmail!.bounds.height)

        mName = UITextField(frame: CGRect(x: PADDING,
                                          y: innerY,
                                          width: mEmail!.bounds.width,
                                          height: mEmail!.bounds.height))
        mName!.textContentType = .nickname
        mName!.autocapitalizationType = .none
        mName!.keyboardType = .default
        mName!.layer.borderColor = ColorDef.background.cgColor
        mName!.layer.borderWidth = 1
        mName!.layer.cornerRadius = 4
        mName!.placeholder = localizedInfoPlist("Nickname")
        mName!.clearButtonMode = .whileEditing
        mName!.returnKeyType = .next
        mName!.delegate = self
        mInputArea!.addSubview(mName!)
        innerY += (PADDING + mEmail!.bounds.height)

如何在昵称 UITextField 的键盘中获取地球按钮?

试试这个

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var mEmail: UITextField!
    @IBOutlet weak var mName: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        mEmail!.textContentType = .emailAddress
        mEmail!.autocapitalizationType = .none
        mEmail!.keyboardType = .emailAddress
        mEmail!.layer.borderWidth = 1
        mEmail!.layer.cornerRadius = 4
        mEmail!.placeholder = "Account_Email"
        mEmail!.clearButtonMode = .whileEditing
        mEmail!.returnKeyType = .next
        mEmail!.delegate = self

        mName!.textContentType = .nickname
        mName!.autocapitalizationType = .none
        mName!.keyboardType = .default
        mName!.layer.borderWidth = 1
        mName!.layer.cornerRadius = 4
        mName!.placeholder = "Nickname"
        mName!.clearButtonMode = .whileEditing
        mName!.returnKeyType = .next
        mName!.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
        switch textField
        {
        case self.mEmail:
            self.mName.becomeFirstResponder()
            break
        default:
            textField.resignFirstResponder()
        }
        return true
    }
}

我发现我的软键盘是英文和 Gboard(Google 键盘)。 当第二个 UITextField 获得焦点时,地球按钮将不会显示。 我认为这可能是 Gboard 不像其他语言键盘 iPhone 那样算的问题。所以我用 "Apple iPhone Keyboard" 添加其他语言然后它显示地球按钮。