Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 clicked on the text field. Below is the screenshot of the design. I want the 'header' to remain on top.

inspector layout for the view

From their documentation, there is a way to keep the navigation bar remain on top.

like image 844
Gijo Varghese Avatar asked Feb 06 '23 21:02

Gijo Varghese


1 Answers

Disable the IQKeyboardManager for your ViewController.

for that,

IQKeyboardManager.sharedManager().disableInViewControllerClass(ViewController.self)

And In that viewController write the following code. It will move your view up as per keyboard height

override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.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
            }
        }
}

Now you want your "HEADER" view remain on TOP then,

Do like this :

**

YourViewController.view -> [headerView][contentView]

**

Put textfield in [contentView] And change [contentView].y instead of Self.view in above code.

like image 76
Wolverine Avatar answered Apr 26 '23 02:04

Wolverine