【问题标题】: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;
        })
    }

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

【问题讨论】:

    标签: ios swift uiview uikeyboard


    【解决方案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()
              })
          }
      

      输出:

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 2020-04-28
        • 2017-08-09
        • 2013-05-21
        • 1970-01-01
        • 2015-02-02
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多