Keep a view always on top (Don't scroll with keyboard) in IQKeyboardManager

前端 未结 2 1054
抹茶落季
抹茶落季 2021-01-19 05:47

I\'m using IQKeyboardManager to keep the text fields to go up after typing with the keyboard.

I don\'t want to scroll to a specific view even when click

2条回答
  •  半阙折子戏
    2021-01-19 06:16

    Disable the IQKeyboardManager for your viewController:

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            IQKeyboardManager.sharedManager().enable = false
    
            NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    

    Handle keyboard:

    func keyboardWillShow(notification: NSNotification) {
    
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                if self.view.frame.origin.y == 0{
                self.table_view.frame.origin.y -= keyboardSize.height
                }
            }
        }
    
    func keyboardWillHide(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                if self.view.frame.origin.y != 0{
                self.table_view.frame.origin.y += keyboardSize.height
                }
            }
        }
    

    Remove observer:

    override func viewWillDisappear(animated: Bool) {
            IQKeyboardManager.sharedManager().enable = true
            NSNotificationCenter.defaultCenter().removeObserver(self)    
        }
    

提交回复
热议问题