![](/img/trans.png)
[英]Is there a way for “force” the keyboard to dismiss in iOS Swift programmatically?
[英]iOS 8 Dismiss keyboard for swift
我是swift的初學者,而且我一直在使用此dismissKeyboard()
方法,但不適用於鍵盤擴展名。
@IBAction func donePressed (sender: UIButton) {
dismissKeyboard()
}
誰能告訴我為什么這行不通?
謝謝。
編輯:完整代碼
導入UIKit
類KeyboardViewController:UIInputViewController {
var keyboardView: UIView!
@IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadInterface()
}
func loadInterface() {
var keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)
self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardView)
view.backgroundColor = self.keyboardView.backgroundColor
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput) {
// The app has just changed the document's contents, the document context has been updated.
}
@IBAction func buttonPressed (sender: UIButton) {
let title = sender.titleForState(.Normal)
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText(title!)
}
@IBAction func spacePressed (sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText(" ")
}
@IBAction func deletePressed (sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.deleteBackward()
}
@IBAction func donePressed (sender: UIButton) {
resignFirstResponder()
}
}
這樣嘗試
self.dismissKeyboard()
也許嘗試:
view.endEditing(true)
從屏幕上關閉自定義鍵盤。
迅速:
self.dismissKeyboard()
Objective-C的:
[self dismissKeyboard];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.