简体   繁体   中英

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM