键盘显示通知iOS扩展

时间:2019-01-18 06:55:15

标签: ios swift nsnotificationcenter

我正在使用此代码。如果出现键盘,则会增加视图的大小,因此用户可以轻松滚动至底部。一切正常,但我想对此代码进行扩展,因为我不想在控制器中使用如此长的代码

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

1 个答案:

答案 0 :(得分:0)

扩展将新功能添加到现有的类,结构,枚举或协议类型。

扩展语法:

  const meetupRefreshToken = (req, res) => {
   return helperFunctionsAxios.refreshingAccessToken("meetup", req, res)
        .then(response => {
            let expiryTime = authHelper.calculatingTokenExpiryTime(response.data.expires_in)   
            let TokenToStore = {"meetupRefreshToken": response.data.refresh_token, "meetupAccessToken": response.data.access_token, "meetup_expires_in": expiryTime }
            FirebaseHelper.updateDataInDb("authentication", req.user.email, TokenToStore)
            .catch(err => {throw err})   
            req.session.contextToken = {...req.session.contextToken, ...TokenToStore}
        })
        .catch(error => {
            errorCodesHelper.errorStatusCodeAndResponseMeetupLoggedInUser(req, res, error)
        }) 
    }