Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Can';t关闭iPhone 5和iPhone 5s的键盘_Ios_Swift_Keyboard - Fatal编程技术网

Ios Can';t关闭iPhone 5和iPhone 5s的键盘

Ios Can';t关闭iPhone 5和iPhone 5s的键盘,ios,swift,keyboard,Ios,Swift,Keyboard,我试图以编程方式消除屏幕键盘,我的代码如下所示 override func viewDidLoad() { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); NSNotificationCenter.defaultCenter().addO

我试图以编程方式消除屏幕键盘,我的代码如下所示

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}

func keyboardWillShow(notification: NSNotification) {
    var info = notification.userInfo!
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.Bottomspace.constant = keyboardFrame.size.height
    })
}

func keyboardWillHide(notification: NSNotification){
    self.Bottomspace.constant = 0
}
它适用于iPhone 6和iPad,但不适用于iPhone 5和iPhone 5s,有什么建议吗?

试试这个:

 // Dismiss Keyboard when user touch screen
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
//当用户触摸屏时关闭键盘
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
self.view.endEditing(true)
}
让我知道它是否有效

试试这个:

 // Dismiss Keyboard when user touch screen
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
//当用户触摸屏时关闭键盘
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
self.view.endEditing(true)
}

让我知道它是否有效

将UITextFieldDelegate添加到类声明中:

class ViewController: UIViewController, UITextFieldDelegate
连接文本字段或以编程方式编写

@IBOutlet weak var userText: UITextField!
将视图控制器设置为视图中委托加载的文本字段:

override func viewDidLoad() {
super.viewDidLoad()
self.userText.delegate = self
}
添加此代理

func textFieldShouldReturn(userText: UITextField!) -> Bool {
yourtextfield.resignFirstResponder()
return true;
}

将UITextFieldDelegate添加到类声明中:

class ViewController: UIViewController, UITextFieldDelegate
连接文本字段或以编程方式编写

@IBOutlet weak var userText: UITextField!
将视图控制器设置为视图中委托加载的文本字段:

override func viewDidLoad() {
super.viewDidLoad()
self.userText.delegate = self
}
添加此代理

func textFieldShouldReturn(userText: UITextField!) -> Bool {
yourtextfield.resignFirstResponder()
return true;
}

当你点击UITextField外部时?不,我点击键盘上的返回键。当你点击UITextField外部时?不,我点击键盘上的返回键