cb428
cb428

Reputation: 495

Move UIButton above keyboard

I'm trying to adjust the y origin of my sign up button to be just above the keyboard. However, this code isn't doing the trick (I don't have auto layout constraints btw)

In viewDidLoad:

// listen for keyboard height
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

Later I have this function:

// keyboard height adjust
func keyboardWillShow(notification: NSNotification) {

    let keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    signUpButton.frame.origin.y = keyboardFrame.origin.y - signUpButton.frame.height

}

Upvotes: 0

Views: 1233

Answers (1)

Duncan C
Duncan C

Reputation: 131398

Don't look at the keyboard's y position. Look at it's height. You can assume it will come from the bottom of the screen, so you need to shift your button up enough so that it's Y position is < keyboard_height+button_height (And I would add a few pixels of space so the button isn't touching the edge of the keyboard. 5 pixels is a good minimum.)

Upvotes: 2

Related Questions