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

前端 未结 2 1025
抹茶落季
抹茶落季 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:07

    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.

    0 讨论(0)
  • 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)    
        }
    
    0 讨论(0)
提交回复
热议问题