简体   繁体   English

将UIButton移至键盘上方

[英]Move UIButton above keyboard

I'm trying to adjust the y origin of my sign up button to be just above the keyboard. 我试图将“注册”按钮的y原点调整为刚好在键盘上方。 However, this code isn't doing the trick (I don't have auto layout constraints btw) 但是,这段代码并不能解决问题(顺便说一句,我没有自动布局约束)

In viewDidLoad: 在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. 不要看键盘的y位置。 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.) 您可以假定它将来自屏幕底部,因此您需要将按钮向上充分移动,以使其Y位置小于<keyboard_height + button_height(而且我会添加一些像素空间,以便按钮不会触碰到键盘边缘。最小为5像素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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