复制:
大概键盘颜色会更改为与背景匹配。但是,在这种情况下,某些键仍然不显示,因此这似乎是iOS中的错误(请参见随附的屏幕截图)。
任何人都想对此有所了解吗?我们正在使用一种解决方法,其中涉及隐藏键盘并重新显示它,但这不是理想的选择。
最佳答案
按下主屏幕按钮时,此代码可以关闭键盘,并在应用重新启动时将其带回。您需要将UITextFields 委托(delegate)设置为 View Controller :
class ViewController: UIViewController, UITextFieldDelegate {
private var _textField: UITextField!
private var _isFirstResponder: Bool!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "didBecomeActiveNotification:", name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "willResignActiveNotification:", name: UIApplicationWillResignActiveNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func didBecomeActiveNotification(nofication: NSNotification) {
if _isFirstResponder? == true {
_textField?.becomeFirstResponder()
}
}
func willResignActiveNotification(nofication: NSNotification) {
if _textField?.isFirstResponder() == true {
_isFirstResponder = true
_textField?.resignFirstResponder()
} else {
_isFirstResponder = false
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
_textField = textField
return true
}
}