0

In my extension I put the control for the keyboard notification in this way

protocol KeyboardSpaceProtocol {
    func addObservers()
    func removeObservers()
    func keyboardWillShow(notification:NSNotification) -> CGFloat
}

extension UIView: KeyboardSpaceProtocol {

    func addObservers() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
    }

    func removeObservers() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification:NSNotification) -> CGFloat {
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        return keyboardRectangle.height
    }
}

now I want update a CGFloat value (keyboardheight) in my UIView class... how can I in my view controller? I don't know when the keyboard go out and how to update my value. Any suggestions?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • quite confusing. You need a method to track when keyboard go out? Then you need subscribe view for UIKeyboardWillHideNotification or UIKeyboardDidHideNotification – alexey.metelkin Aug 08 '16 at 05:10

2 Answers2

0

First, the notification call back methods generally don't require to return any values. In this case 'keyboardWillShow' 's return value is ignored and is not used by the notification centre. It simply calls this method when the notification is sent.

To record the height of the keyboard, you will need to have a member variable in your view class or view controller class that is conforming to this protocol.

If your view controller too requires to get notified, that also need to conform to this protocol.

For instance in your view class you can track the height something like this-

class MyView : UIView {
  var keyboardHeight : CGFloat = 0.0 

  //Override the version defined in UIView extension here. 
  override func keyboardWillShow(notification:NSNotification){
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        keyboardHeight =  keyboardRectangle.height
  }

}

HTH..

Shripada
  • 6,296
  • 1
  • 30
  • 30
0

You can use objc_associatedObject to add properties at run time.

My example uses UIViewController instead of UIView just because it seems more logical to me.

import Foundation
import UIKit
import ObjectiveC

protocol KeyboardSpaceProtocol {
    func addObservers()
    func removeObservers()
    func keyboardWillShow(notification:NSNotification) -> CGFloat
    var keyBoardHeight: CGFloat {get set}
}

private var keyBoardKey: UInt8 = 0
extension  UIViewController: KeyboardSpaceProtocol {

    var keyBoardHeight: CGFloat {
        get {
            return objc_getAssociatedObject(self, &keyBoardKey) as! CGFloat
        }
        set { objc_setAssociatedObject(self, &keyBoardKey, newValue,  objc_AssociationPolicy(rawValue: 0)!) }
    }

    func addObservers() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(UIViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
    }

    func removeObservers() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification:NSNotification) -> CGFloat {
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()

        var selfAsProtocol = self as KeyboardSpaceProtocol
        selfAsProtocol.keyBoardHeight = keyboardRectangle.height

        return keyboardRectangle.height
    }
}
MCMatan
  • 8,623
  • 6
  • 46
  • 85