Always keep the view on top (do not scroll the keyboard) in IQKeyboardManager

I use IQKeyboardManagerto save text fields after keyboard input.

I don’t want to switch to a specific view even when I click on a text field. Below is a screenshot. I want the title to stay on top.

inspector layout for presentation

From their documentation, there is a way to keep the navigation bar up to par.

+1
source share
2 answers

Try it. Disable the IQKeyboardManager for this viewController.

for this,

IQKeyboardManager.sharedManager().disableInViewControllerClass(your view controller class name here)

And in this viewController write the following code. It will move your view up the height of the keyboard.

override func viewDidLoad() {
        super.viewDidLoad()

        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)

}

func keyboardWillShow(notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0{
            self.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.view.frame.origin.y += keyboardSize.height
        }
        }
    }

, HEADER TOP,

:

**

YourViewController.view → [headerView] [contentView]

**

[contentView] [contentView].y Self.view .

+2

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

:

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

:

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

Source: https://habr.com/ru/post/1658170/


All Articles