ios - iOS 键盘显示通知扩展

标签 ios swift nsnotificationcenter

我正在使用这个代码。如果出现键盘,则会增加 View 的大小,以便用户可以轻松滚动到底部。一切正常,但我想对此代码进行扩展,因为我不想在我的 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)
        }
}

最佳答案

扩展向现有类、结构、枚举或协议(protocol)类型添加新功能。

扩展语法:

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

关于ios - iOS 键盘显示通知扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249053/

相关文章:

iphone - 如何知道 GLCameraRipple 示例中的涟漪何时消失?

iPhone (iOS) 应用程序使用本地 sqlite 并希望在多个设备之间同步

ios - swift ios 如何在 swift 中访问 AnyHashable 数据

objective-c - NSNotificationCenter - 使用多种方法观察通知名称

ios - 在xamarin中获取NSNotification的观察者

objective-c - CALayer setNeedsDisplay 无法工作?

ios - 如何不在标签中显示 0?

ios - 需要从 NSArray 或 NSMutableArray 存储/检索对象

ios - 让 AVPlayer 填满整个屏幕

ios - 避免 JSONSerialization 将 Bool 转换为 NSNumber