简体   繁体   中英

View Responsiveness on Keyboard Show and Hide Ios Swift 4

This is my view.

在此处输入图片说明

When I click inside the text view the keyboard was coming on top. so I added made a class and in that class, I added these functions.

    var objectObserver:UIViewController?

    func setKeyboardResponsiviness(observer:UIViewController){
            objectObserver = observer
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

                NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
        }

        @objc func keyboardWillShow(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                if objectObserver!.view.frame.origin.y == 0 {
                    objectObserver!.view.frame.origin.y -= keyboardSize.height
                }
            }
        }

        @objc func keyboardWillHide(notification: NSNotification) {
            if objectObserver!.view.frame.origin.y != 0 {
                objectObserver!.view.frame.origin.y = 0
            }
        } 

After adding the code the whole screen slides up which was the intended goal but as a side effect, half of the text view is out of the screen. Any idea how I can fix this?

The simple solution is to use IQKeyboardManagerSwift.

pod 'IQKeyboardManagerSwift' // add this in your pod file.

Add the following code in didFinishLaunchingWithOptions.

IQKeyboardManager.shared.enable = true

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM