键盘出现动作

Keyboard appearing action

我正在尝试制作一个 UIView,其中包含 UITextFieldUIButton -喜欢聊天应用程序。

我想检测屏幕上出现的键盘级别并根据它更改 UIView 高度约束。

我该怎么做?

现在我有了这个

  @IBOutlet weak var messageTextField: UITextField!
    @IBOutlet weak var bottomBarConstrains: NSLayoutConstraint!

    override func viewDidLoad()
    {
        super.viewDidLoad()

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

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func sendAction(sender: AnyObject)
    {
        messageTextField.resignFirstResponder();
    }

    func keyboardWillShow(notification: NSNotification)
    {
        var info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! Double

        UIView.animateWithDuration(duration, delay: 2, options: UIViewAnimationOptions.CurveEaseIn, animations:
            { () -> Void in
                self.bottomBarConstrains.constant = keyboardFrame.size.height;
            },
            completion: nil);
    }

    func keyboardWillHide(notification: NSNotification)
    {
        var info = notification.userInfo!
        let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! Double

        UIView.animateWithDuration(duration, animations: { () -> Void in
            self.bottomBarConstrains.constant = 0;
        })
    }

但它发生在没有动画的情况下

如果要为更改的约束设置动画,则需要在使用该约束的视图上使用 layoutIfNeeded 方法。此方法强制视图更改布局子视图,但仅在需要时才这样做。由于约束更改不会自动强制更改视图位置,因此您需要调用此方法。因此,如果您的 messageTextViewself.view 的子视图,请使用:

self.bottomBarConstrains.constant = keyboardFrame.size.height

UIView.animateWithDuration(duration, delay: 2, options: .CurveEaseIn, animations: {
     self.view.layoutIfNeeded()
}, completion: nil);

更改键盘打开和隐藏的代码如下,打开键盘时删除延迟,

func keyboardWillShow(notification: NSNotification)
    {

        var info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
        self.bottomBarConstrains.constant = keyboardFrame.size.height

        UIView.animateWithDuration(duration, animations: { () -> Void in

            self.messageTextField.layoutIfNeeded()
            self.view.layoutIfNeeded()
        })
    }

    func keyboardWillHide(notification: NSNotification)
    {
        var info = notification.userInfo!
        let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
        self.bottomBarConstrains.constant = 0;

        UIView.animateWithDuration(duration, animations: { () -> Void in

            self.messageTextField.layoutIfNeeded()
            self.view.layoutIfNeeded()
        })
    }

输出: