【问题标题】:View disappears that slides up when textfield is selected, disappears when keyboard is tapped选择文本字段时向上滑动的视图消失,点击键盘时消失
【发布时间】:2018-02-03 02:02:27
【问题描述】:

我正在尝试复制 whatsapp、imessage 等键盘,发送按钮/文本字段位于其顶部 我有一个 TableView 和一个包含文本字段和发送按钮的容器视图。

每次我点击文本字段时,容器视图都会上升并“坐在”键盘上,但是一旦我开始输入,容器视图就会消失并进入最初的底部。为什么会这样??

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

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    您的代码中的问题是您在键盘 show/hide 中增加了容器视图的 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()
    
        })
    
    }
    

    【讨论】:

    • 它实际上使情况变得更糟!现在不仅在敲击键盘后视图仍然消失,而且每次选择文本字段时它也会越来越低
    • 同样的结果!我不认为这是问题所在,因为我的解决方案是键盘按预期工作,问题是容器视图(包含文本字段和发送按钮)在敲击键盘时滑落到初始位置。
    • 尝试评论 view.endEditing(true)
    • 我已经这样做了,我还删除了整个touchesBegan。什么都没有改变..
    • y 当键盘要隐藏时增加 y self.containerView.frame.origin.y += keyboardSize.height
    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 2016-11-20
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多