简体   繁体   English

选择文本框时,视图消失并向上滑动;点击键盘时,视图消失

[英]View disappears that slides up when textfield is selected, disappears when keyboard is tapped

I am trying to replicate whatsapp, imessage etc. keyboard going up with the send button/textfield sitting right on top of it I have one TableView and a container view that holds the textfield and the send button. 我正在尝试复制whatsapp,message等键盘,将发送按钮/文本字段放在其顶部,我有一个TableView和一个容纳文本字段和发送按钮的容器视图。

Everytime i tap on the textfield the container view goes up and "sits" on the keyboard, BUT once i start typing the container view disappears and goes to the bottom where initially was. 每次我在文本字段上点击时,容器视图都会上升,并在键盘上“坐下”,但是一旦我开始键入容器视图,容器视图就会消失并进入最初的底部。 Why is this happening?? 为什么会这样?

override func viewDidLoad() {
messageTextfield.delegate=self
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}

@objc func keyboardWillShow(_ sender: Notification) {

   let duration = sender.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = sender.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let beginningFrame = (sender.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endFrame = (sender.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let deltaY = endFrame.origin.y - beginningFrame.origin.y

    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
        self.containerView.frame.origin.y += deltaY
    }, completion: nil)

}


@objc func keyboardWillHide(_ sender: Notification) {
    let userInfo: [AnyHashable: Any] = sender.userInfo!
    let keyboardSize: CGSize = (userInfo[UIKeyboardFrameBeginUserInfoKey]! as AnyObject).cgRectValue.size
    self.containerView.frame.origin.y += keyboardSize.height

}
override func viewDidDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}



func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){

   super.touchesBegan(touches, with: event)
    view.endEditing(true)
}

The problem in your code is that you increase y position of container view in both keyboard show/hide , so Change to this 您的代码中的问题是您在两个键盘显示/隐藏中都增加了容器视图的y位置,因此请更改为此

 @objc  func handleKeyboardDidShow (notification: NSNotification)
 {
    let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue

    var keyboardRect = CGRect.zero

    keyboardRectAsObject.getValue(&keyboardRect)

    self.containerViewBotcon.constant = -1 * keyboardRect.height 

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

} 

@objc func handleKeyboardWillHide(notification: NSNotification)
{

    self.containerViewBotcon.constant = 0

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM