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

折月煮酒 提交于 2019-12-31 02:58:24

问题


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.

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


回答1:


Try this. Disable the IQKeyboardManager for your this viewController.

for that,

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

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

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

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.




回答2:


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


来源:https://stackoverflow.com/questions/40124131/keep-a-view-always-on-top-dont-scroll-with-keyboard-in-iqkeyboardmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!