#define kOFFSET_FOR_KEYBOARD 280.0
- (void)keyboardWillHide:(NSNotification *)notif {
[self setViewMoveUp:NO];
}
- (void)keyboardWillShow:(NSNotification *)notif{
[self setViewMoveUp:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
stayup = YES;
[self setViewMoveUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
stayup = NO;
[self setViewMoveUp:NO];
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMoveUp:(BOOL)moveUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (moveUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
if (rect.origin.y == 0) {
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//rect.size.height += kOFFSET_FOR_KEYBOARD;
}
}
else
{
if (stayup == NO) {
rect.origin.y += kOFFSET_FOR_KEYBOARD;
//rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
이 방법을 시도해보십시오. 귀하의 요구 사항에 따라 편집하십시오.
좋아요, 당신의 코드를 사용했는데, 나는 void, IBAction 대신 put을하고 textedit에 이벤트를 할당해야했습니다. 여전히 keyboardWillHide와 keyboardWillShow의 역할을 알지 못합니다. 그들을 사용하는 방법, .., 키보드를 숨기기위한 내가 사용 http://stackoverflow.com/questions/2586937/how-to-hide-the-keyboard-programatically-in-iphone. 이 멋진 솔루션을 주셔서 감사합니다 – PartySoft
textFieldDidBeginEditing 및 textFieldDidEndEditing을 사용하는 경우 애니메이션 타이밍이 완벽하지 않을 수 있으므로 keyboardWillShow 및 keyboardWillHide를 사용하여 적절하게 타이밍을 조정할 수 있습니다. 이 두 가지 방법은 조금 일찍 발생합니다. 어쨌든 귀하의 경우에는 textFieldDidBeginEditing 및 textFieldDidEndEditing 충분하다고 생각합니다. :) –
stayup = YES; 여기에 머물러있는 것이 무엇입니까? –