简体   繁体   中英

extension for keyboard show notification iOS

I'm using this code. if keyboard appears then it increases the size of the view so user can easily scroll to the bottom. everything works fine but I want to make an extension for this code because I don't want to use such a long code in my controller

import UIKit

    extension UIViewController {


        func hideKeyboardWhenTappedAround() {
            let tap: UITapGestureRecognizer =     UITapGestureRecognizer(target: self, action:    #selector(dismissKeyboard))
            tap.cancelsTouchesInView = false
            view.addGestureRecognizer(tap)
        }


        @objc func dismissKeyboard() {
            view.endEditing(true)
        }

        func setnotification()
        {

            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown), name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        }

        @objc func keyboardWasShown(notification: NSNotification)
        {

            var info = notification.userInfo!
            let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
            let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height+10, right: 0.0)
            self.scrolView.contentInset = contentInsets
            self.scrolView.scrollIndicatorInsets = contentInsets
            var aRect : CGRect = self.view.frame
            aRect.size.height -= keyboardSize!.height
            if let activeField =  activeTextField
            {
                if (!aRect.contains(activeField.frame.origin))
                {
                   self.scrolView.scrollRectToVisible(activeField.frame, animated: true)
                }
            }
        }
        // when keyboard hide reduce height of scroll view
        @objc func keyboardWillBeHidden(notification: NSNotification){
            let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0,bottom: 0.0, right: 0.0)
            myScrolView!.contentInset = contentInsets
            myScrolView!.scrollIndicatorInsets = contentInsets
            self.view.endEditing(true)
        }
}

Extensions add new functionality to an existing class, structure, enumeration, or protocol type.

Syntax for extension:

    extension ViewControllerName {
      // Put code which you want to 
    }      

   /**
     If you implement UITableViewDataSource and UITableViewDelegate methods 
     or you can implement for UIPickerViewDataSource methods and protocol also    
  */

   extension ViewController: UItableViewDataSource, UITableViewDelegate {

     //implement tableview datasource and delegate method 

   }

      /**
          Keyboard show/hide 
      */  
      extension ViewController {
       /**
           Add scrollview functionality to scroll top and you can call 
           this function from anywhere in the controller.
       */
       func scrollToTop() {

       } 


        @objc func keyboardWasShown(notification: NSNotification)
            {
                var info = notification.userInfo!
                let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
                let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height+10, right: 0.0)
                self.scrolView.contentInset = contentInsets
                self.scrolView.scrollIndicatorInsets = contentInsets
                var aRect : CGRect = self.view.frame
                aRect.size.height -= keyboardSize!.height
                if let activeField = self.activeTextField
                {
                    if (!aRect.contains(activeField.frame.origin))
                    {
                        self.scrolView.scrollRectToVisible(activeField.frame, animated: true)
                    }
                }
            }
            // when keyboard hide reduce height of scroll view
            @objc func keyboardWillBeHidden(notification: NSNotification){
                let contentInsets : UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0,bottom: 0.0, right: 0.0)
                self.scrolView.contentInset = contentInsets
                self.scrolView.scrollIndicatorInsets = contentInsets
                self.view.endEditing(true)
            }
        }

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