4

I'm trying to implement a simple keyboard observer in my iOS 8 Swift app but it really doesn't work. This is the code im currently using:

override func viewDidAppear(animated: Bool) {
    NSNotificationCenter().addObserver(self, selector: Selector(keyboardWillAppear()), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter().addObserver(self, selector: Selector(keyboardWillHide()), name: UIKeyboardWillHideNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter().removeObserver(self)
}

func keyboardWillAppear() {
    logoHeightConstraint.constant = 128.0
}

func keyboardWillHide() {
    logoHeightConstraint.constant = 256.0
}

Strangely both functions to react to the keyboard are called once after starting the app. Nothing happens when I enter or leave a textfield. What am I doing wrong? And by the way: Is altering a constraint the best solution for changing the size of an image?

I really appreciate your help!

stoeffn
  • 556
  • 3
  • 9
  • 18
  • 1
    I could be wrong, but I believe that `NSNotificationCenter()` is instantiating a new NSNotificationCenter each time you call it. Have you tried using `NSNotificationCenter.defaultCenter()`? – Patrick Goley Jul 28 '14 at 18:14

5 Answers5

8

Calling NSNotificationCenter() is instantiating a new NSNotificationCenter each time you call it. Try using NSNotificationCenter.defaultCenter() instead.

Patrick Goley
  • 5,397
  • 22
  • 42
8
//keyboard observers 
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)




func keyboardWillAppear() {
    println("Keyboard appeared")
}

func keyboardWillHide() {
    println("Keyboard hidden")
}
Logan
  • 52,262
  • 20
  • 99
  • 128
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • 1
    I am sorry to say this does not work, since it is a Objective C-selector. #selector(keyboardWillAppear) #selector(keyboardWIllHide) works for me see this post: http://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift – Werner Kratochwil Aug 10 '16 at 08:34
  • Of course it works, and does work hence its the highest rated answer. Please qualify your statement of "it is a Objective C-selector" when it's written in swift – JSA986 Aug 10 '16 at 09:27
  • with the code given a get this warning in XCode: Use #selector instead of explicitly constructing a 'Selector' furthermore println is not valid anymore (I get an error) and should be replaced with print – Werner Kratochwil Aug 10 '16 at 12:48
  • 1
    Thats because at the time it was answered (2 years ago) it was correct for the version current version of Swift at the time. – JSA986 Aug 10 '16 at 20:20
1

I prefer to use UIKeyboardWillChangeFrameNotification, because you get informed if the size changes, if e.g. when suggestions are hidden/shown

//keyboard observers 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillChange), name: UIKeyboardWillChangeFrameNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)

func keyboardWillChange(notification:NSNotification)
{
    print("Keyboard size changed")
}

func keyboardWillHide(notification:NSNotification)
{
    print("Keyboard hidden")
}
1

Swift 3 and above code. Added code for getting height for keyboard

func addObservers() {
    //keyboard observers
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidAppear(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

@objc func keyboardDidAppear(notification: NSNotification) {
    print("Keyboard appeared")
    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    print("Keyboard size: \(keyboardSize)")

    let height = min(keyboardSize.height, keyboardSize.width)
    let width = max(keyboardSize.height, keyboardSize.width)
    print(height)
    print(width)
}

@objc func keyboardWillHide(notification: NSNotification) {
    print("Keyboard hidden")
}
Van
  • 1,225
  • 10
  • 18
1

Swift 4.0, closures:

NotificationCenter.default.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil, using: { notification in
   // do stuff
})
General Grievance
  • 4,555
  • 31
  • 31
  • 45
schirrmacher
  • 2,341
  • 2
  • 27
  • 29