我想更新
我希望更改
是否有一种方法在VC中强制重新检查?
感谢您的帮助!
ViewController
中的UIKeyboardAppearance
。我的意思是,假设VC加载时使用的是UIKeyboardAppearance.default
,如果我按下按钮,我想将键盘更新为.dark
,并且在同一VC中以.dark
显示键盘。
据我所知,iOS在加载VC时会检查UIKeyboardAppearance
的值,并且直到再次加载VC之前不会再次检查。即使您更改了UIKeyboardAppearance
的值并隐藏/显示了键盘。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()
}
我希望更改
UIKeyboardAppearance
并隐藏/显示键盘后,它能以新的外观(.dark
或.default
)显示,但直到重新加载VC之前,它仍然以相同的外观显示。您可以看到UIKeyboardAppearance
的值已更改,但是iOS似乎直到VC再次加载才会检查该更新。是否有一种方法在VC中强制重新检查?
感谢您的帮助!