本文主要是介绍完美处理弹出键盘,界面上移功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 定义int m_curKeyboardHeight;
在init函数中 设置为 m_curKeyboardHeight =0;
2. 在init中 注册两个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
在dealloc中,注销这两个通知:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
3. 通知响应函数 的定义以及实现。
假设 输入框为
UITextField * m_inputField;
- (void)keyboardWillShow:(NSNotification *)aNotification;
- (void)keyboardWillHide:(NSNotification *)aNotification;
- (void)keyboardWillShow:(NSNotification *)aNotification
{
if ([m_inputField isFirstResponder] == NO)
{
return;
}
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bottomframe = m_bottomImage.frame;
bottomframe.origin.y -= keyboardRect.size.width - m_curKeyboardHeight;
CGRect fieldframe = m_inputField.frame;
fieldframe.origin.y -= keyboardRect.size.width - m_curKeyboardHeight;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
m_bottomImage.frame = bottomframe;
m_inputField.frame = fieldframe;
[UIView commitAnimations];
m_curKeyboardHeight = keyboardRect.size.width;
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
if ([m_inputField isFirstResponder] == NO)
{
return;
}
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bottomframe = m_bottomImage.frame;
bottomframe.origin.y += m_curKeyboardHeight;
CGRect fieldframe = m_inputField.frame;
fieldframe.origin.y+= m_curKeyboardHeight;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
m_bottomImage.frame = bottomframe;
m_inputField.frame = fieldframe;
[UIView commitAnimations];
m_curKeyboardHeight = 0;
}
这篇关于完美处理弹出键盘,界面上移功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!