Keeping object on top of keyboard in the event of becomeFirstResponder or resignFirstResponder?

前端 未结 4 2092
感情败类
感情败类 2020-11-28 05:05

I currently have a UITextField on top of a keyboard. When you tap it, it should stick on top of the keyboard and move up smoothly. I don\'t know the exact duration and anima

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 05:55

    Use this code in your keyboard notification:

    @objc func keyboardWillShowNotification(notification: Notification) {
        let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height ?? 216
        let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.25
        let curve = (notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UInt ?? 7) << 16
        tableViewBottomConstraint.constant = keyboardHeight - submitButtonContainer.frame.size.height
    
        UIView.animate(withDuration: duration, delay: 0, options: [UIViewAnimationOptions(rawValue: curve)], animations: {
            // Animation
        }, completion: nil)
    }
    

提交回复
热议问题