键盘出现动作

Posted

技术标签:

【中文标题】键盘出现动作【英文标题】:Keyboard appearing action 【发布时间】:2015-11-17 12:19:05 【问题描述】:

我正在尝试制作一个 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;
        )
    

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

【问题讨论】:

【参考方案1】:

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

self.bottomBarConstrains.constant = keyboardFrame.size.height

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

【讨论】:

【参考方案2】:

修改KeyBoard Open and Hide的代码如下,去掉打开KeyBoard时的延迟,

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()
        )
    

输出:

【讨论】:

以上是关于键盘出现动作的主要内容,如果未能解决你的问题,请参考以下文章

Android:捕捉按下键盘输入按钮的动作

UITextField 的键盘不会出现在 Swift 中的第一次点击时

高分求教按键精灵2个脚本代码,即颜色符合执行鼠标动作。

使用java更改焦点窗口

java键盘录入

点击手势识别器不会触发