3

Been looking at several SO post now, but none seem to help me. Just want a UITableViews bottom constraint to be set so that the keyboard never is on top of the tableview. Seems impossible in iOS 8. It work when using normal soft keyboard, but when removing it in simulator or using real hardware keyboard, it still thinks there is a soft keyboard.

The code:

var currentKeyboardHeight:CGFloat = 0.0

override public func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()

        let windowRect = view.window!.convertRect(keyboardRect, fromWindow:nil)
        let viewRect = view.convertRect(windowRect, fromView:nil)

        let deltaHeight = viewRect.size.height - currentKeyboardHeight

        currentKeyboardHeight = viewRect.size.height
        tableViewBottomConstraint.constant = currentKeyboardHeight
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })

        println("show keyboard: \(currentKeyboardHeight)")
    }
}

func keyboardWillHide(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue()
        keyboardRect = self.view.convertRect(keyboardRect, fromView: nil)

        let deltaHeight = keyboardRect.size.height - currentKeyboardHeight
        currentKeyboardHeight = 0
        tableViewBottomConstraint.constant = currentKeyboardHeight

        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.view.layoutIfNeeded()
        })

        println("hideKeyboard \(currentKeyboardHeight)")

    }
}

When selecting a uitextview in the last tableviewrow only this is printed:

show keyboard: 260.0

which is Wrong. If then you turn on soft keyboard (cmd+shift+k):

show keyboard: 260.0
show keyboard: 297.0
show keyboard: 297.0

which is Correct. And then turn it off (cmd+shift+k):

hideKeyboard 0.0
show keyboard: 260.0
hideKeyboard 0.0

which is Correct

Update 1:

According to the comment on this answer (https://stackoverflow.com/a/2893327/511299) the UIKeyboardWillShowNotification will always fire when using a accessoryView which I am.

func setupKeyboardButtons()
{
    let numberToolbar = UIToolbar(frame: CGRectMake(0,0,320,50))
    numberToolbar.barStyle = UIBarStyle.Default

    let button1 = UIBarButtonItem(title: CANCEL_TEXT, style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardCancelButtonTapped:")
    let button2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let button3 = UIBarButtonItem(title: SAVE_TEXT, style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardDoneButtonTapped:")

    button1.tintColor = UIColor.darkGrayColor()

    numberToolbar.items = [button1, button2, button3]

    numberToolbar.sizeToFit()
    commentTextView.inputAccessoryView = numberToolbar
}
Community
  • 1
  • 1
Sunkas
  • 9,542
  • 6
  • 62
  • 102

1 Answers1

1

You can detect if external keyboard is connected and and set keyboard's height to 0 by doing something like that.

Objective-C version:

CGRect finalKeyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
finalKeyboardFrame = [self convertRect:finalKeyboardFrame fromView:self.window];

NSLog(@"origin.y: %f", finalKeyboardFrame.origin.y);

Shows the y coordinate properly depending if external keyboard is connected or not:

origin.y: 258.000000 (not connected)

origin.y: 474.000000 (connected)

Community
  • 1
  • 1
Artrmz
  • 340
  • 2
  • 14
  • Thanks. They seem to differ when using external keyboard. But what do they represent and how do I set my bottom constraint from this? – Sunkas Apr 30 '15 at 07:54
  • You can ie. calculate real height of your keyboard: `self.frame.size.height - finalKeyboardFrame.origin.y` – Artrmz Apr 30 '15 at 08:05
  • Thanks, just figured it out myself :) – Sunkas Apr 30 '15 at 08:06