侧边栏壁纸
博主头像
最闪啊姚凌武!博主等级

天下武功,唯快不破

  • 累计撰写 293 篇文章
  • 累计创建 34 个标签
  • 累计收到 10 条评论

目 录CONTENT

文章目录

完美处理弹出键盘,界面上移功能

姚凌武
2014-11-04 / 0 评论 / 0 点赞 / 7 阅读 / 10212 字
int          m_curKeyboardHeight;   在init函数中  设置为 m_curKeyboardHeight =0;   2. 在init中  注册两个通知

        [[NSNotificationCenter defaultCenteraddObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

        [[NSNotificationCenter defaultCenteraddObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

 

      在dealloc中,注销这两个通知:

      

        [[NSNotificationCenter defaultCenterremoveObserver:selfname:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenterremoveObserver:selfname: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 userInfoobjectForKey:UIKeyboardFrameEndUserInfoKeyCGRectValue];  

    NSTimeInterval animationDuration = [[[aNotification userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKeydoubleValue];

    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 userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKeydoubleValue];

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;

}
]]>
0
iOS

评论区