简体   繁体   中英

Handle text input view movement with keyboard pan gesture in iOS Swift

I am developing an iOS app and I have a messaging view where I want to handle this situation: -> I have a input view at the bottom of the view, that needs to be visible all the time except for some criteria where user is blocked/restricted to send message. -> when the input view is focused, keyboard appears, I want to move the view along with keyboard frame. -> I want keyboard to dismiss interactively with table view scrolling. With this being said, the view should respond to keyboard pan gesture and move along with as well -> I tried using input accessory view but problem with that was when keyboard gets dismissed with table view scrolling, input view gets dismissed as well. -> I also tried using willShow/willHide/willChangeFrame observers but with this, response is not to the point and it doesn't respond to keyboard interactive dismissal. Anybody got solution to this... Thanks for your time.

Swift 3+: I have take a view into textview background and set the constraint of view (leading, trailing, bottom, fixed height) . Create a @IBOutlet for bottom constraint and manage that below code:

class ViewController: UIViewController {

    @IBOutlet var bottomConstraint: NSLayoutConstraint!
    @IBOutlet var view_TextViewBg: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(keyboardDidShow(_:)),
            name: NSNotification.Name.UIKeyboardWillShow,
            object: nil)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(keyboardDidHide(_:)),
            name: NSNotification.Name.UIKeyboardWillHide,
            object: nil)

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
        //  tap.delegate = self
        view.addGestureRecognizer(tap)

    }
    func handleTap(sender: UITapGestureRecognizer? = nil) {
        //dissmiss your keyboard here
    }

    //MARK: Keyboard show

    func keyboardDidShow(_ notification: Notification) {
        let params = notification.userInfo
        let rect: CGRect? = (params?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
        bottomConstraint.constant = (rect?.size.height)!
    }

    //MARK: Keyboard hide

    func keyboardDidHide(_ notification: Notification) {
        bottomConstraint.constant = 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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