UITextField输入框怎样跟随UIKeyboard的位置变化而变化(By 老区)

首先在ViewDidLoad里面注册一个UIKeyboardWillChangeFrameNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];

 

然后实现方法 changeKeyboard
- (void)changeKeyboard:(NSNotification*)notification
{
    //NSLog(@"change key board");
    NSDictionary* keyboardInfo = [notification userInfo];
    //NSLog(@"keybaordInfo:%@",keyboardInfo);
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [keyboardFrameBegin CGRectValue];
    //NSLog(@"y:%f height:%f",keyboardFrame.origin.y,keyboardFrame.size.height);
 
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
 
                         vSendMailView.frame = (CGRect){(CGPoint){vSendMailView.frame.origin.x,keyboardFrame.origin.y - vSendMailView.frame.size.height - 64},vSendMailView.frame.size} ;
                     }
                     completion:^(BOOL finished) { }];
}

 

这样UITextField就会跟随键盘的高度变化而变化

posted @ 2013-11-08 16:53  老区  阅读(169)  评论(0)    收藏  举报