有没有办法告诉iOS更新视图控制器中的键盘外观?

有没有办法告诉iOS更新视图控制器中的键盘外观?,ios,swift,uiviewcontroller,uikeyboard,Ios,Swift,Uiviewcontroller,Uikeyboard,我想在ViewController中更新ui键盘外观。我的意思是,假设VC加载的是UIKeyboardAppearance.default。如果我按下一个按钮,我希望键盘更新到.dark,并且现在在与.dark相同的VC中显示键盘 据我所知,iOS在加载VC时会检查UIKeyboardAppearance的值,并且在再次加载VC之前不会再次检查。即使更改ui键盘外观的值并隐藏/显示键盘 import UIKit class ViewController: UIViewController {

我想在
ViewController
中更新
ui键盘外观。我的意思是,假设VC加载的是
UIKeyboardAppearance.default
。如果我按下一个按钮,我希望键盘更新到
.dark
,并且现在在与
.dark
相同的VC中显示键盘

据我所知,iOS在加载VC时会检查
UIKeyboardAppearance
的值,并且在再次加载VC之前不会再次检查。即使更改
ui键盘外观的值并隐藏/显示键盘

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

// creating a simple text box, and making the placeholder text the value of the keyboardAppearance
        myTextBox.backgroundColor = UIColor.lightGray
        myTextBox.frame = CGRect(x: 30, y: 200, width: 300, height: 50)
        view.addSubview(myTextBox)
        UITextField.appearance().keyboardAppearance = .dark
        myTextBox.becomeFirstResponder()
        myTextBox.placeholder = "Keybaoard Appearance is: \(UITextField.appearance().keyboardAppearance.rawValue)"

// a simple button to toggle the keyboardAppearance
        toggleButton.frame = CGRect(x: 30, y: 300, width: 300, height: 50)
        toggleButton.setTitle("Toggle Keyboard", for: .normal)
        toggleButton.backgroundColor = UIColor.red
        toggleButton.addTarget(self, action: #selector(toggleButtonFunction), for: .touchUpInside)
        view.addSubview(toggleButton)

    }

// toggles the keyboardAppearance. Hides the keyboard, and a second later shows it again.
    @objc func toggleButtonFunction() {
        if UITextField.appearance().keyboardAppearance == .dark {
            UITextField.appearance().keyboardAppearance = .default
        }
        else {
            UITextField.appearance().keyboardAppearance = .dark
        }
        myTextBox.resignFirstResponder()
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            self.myTextBox.becomeFirstResponder()
            self.myTextBox.placeholder = "Keybaoard Appearance is: \(UITextField.appearance().keyboardAppearance.rawValue)"

            })
    }

    let myTextBox = UITextField()
    let toggleButton = UIButton()
}
我希望在更改
ui键盘外观
并隐藏/显示键盘后,它会以新的外观显示(
.dark
.default
),但它会以相同的外观持续显示,直到再次加载VC。您可以看到
UIKeyboardAppearance
的值发生了变化,但在VC再次加载之前,iOS似乎不会检查该更新

有没有办法在VC内部强制重新检查


谢谢你的帮助

您可以在屏幕上递归更改所有文本字段的键盘外观(扩展名为
allSubviewsOf(type:)


谢谢塔马斯的回答

它引导我找到我需要的东西

看起来,如果更改UITextField的键盘外观

UITextField.appearance().keyboardAppearance = .dark
系统仅在VC加载时进行检查。如果您为每个文本字段更改它

myTextBox.keyboardAppearance = .dark
每次firstResponder更改并加载正确的键盘时,系统都会进行检查

再次感谢塔玛斯

UITextField.appearance().keyboardAppearance = .dark
myTextBox.keyboardAppearance = .dark