Swift View" Bouncing"关于键盘关闭

时间:2015-08-31 21:12:04

标签: ios swift keyboard hide show

我正在编写一个应用程序,用户必须在文本框中键入单词,然后按一个按钮以查看他们是否输入了正确的单词。当用户点击文本框时,我希望视图向上移动,以便键盘不会阻挡任何内容。我使用以下代码完成此转换,并在键盘解除后返回结果。

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

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

}

func keyboardWillShow(sender: NSNotification) {

    var info:NSDictionary = sender.userInfo!
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    self.view.frame.origin.y -= (keyboardSize.height)
}

func keyboardWillHide(sender: NSNotification) {

    var info:NSDictionary = sender.userInfo!
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    self.view.frame.origin.y += (keyboardSize.height)
}

我遇到的问题是,当用户显示键盘并按下按钮时,键盘将被解除,然后视图会被移动

0 个答案:

没有答案