UIKit은 키보드를 표시 할 때 UIKeyboardWillShowNotification
을 게시하고 키보드를 표시하면 UIKeyboardWillHideNotification
을 표시합니다. 이 알림에는 UITextField
을 올바르게 애니메이션하는 데 필요한 모든 것이 포함되어 있습니다. UITextField
이 myTextField
이라는 속성에 있다고 가정 해 보겠습니다.
먼저 알림을 등록해야합니다. 등록 할 곳은 myTextField
을 움직일 수있는 대상에 따라 다릅니다. 내 프로젝트에서 필드의 수퍼 책임이고, 나는 펜촉에서 내 UI를로드하기 때문에, 나는 수퍼의 awakeFromNib
에서 할 : 당신이 주위의 필드를 이동하는 UIViewController
를 사용하는 경우
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
}
, 당신은 아마거야 viewWillAppear:animated:
에서 해보고 싶습니다. 물론
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
까다로운 비트가 keyboardWillHideOrShow:
방법입니다 :
당신은 당신의 dealloc
또는 viewWillDisappear:animated:
에 등록을 취소한다. 우선은 통지에서 애니메이션 매개 변수를 추출 :
- (void)keyboardWillHideOrShow:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame
글로벌 좌표계에 있습니다. 나는 myTextField.frame
같은 좌표 시스템에 프레임을 변환해야하고, myTextField.frame
는 myTextField.superview
의 좌표계에 있습니다
CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];
다음으로, 나는 myTextField
로 이동하려는 프레임을 계산한다. 키보드를 사용하는, I는 동일한 애니메이션 파라미터를 이용하여, 새로운 프레임에 myTextField
애니메이션 마지막
CGRect newTextFieldFrame = self.myTextField.frame;
newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;
: 새로운 프레임의 하단은 키보드의 프레임의 상단 같아야
여기
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.myTextField.frame = newTextFieldFrame;
} completion:nil];
}
이 모두 함께 넣어 :
- (void)keyboardWillHideOrShow:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];
CGRect newTextFieldFrame = self.myTextField.frame;
newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.myTextField.frame = newTextFieldFrame;
} completion:nil];
}
고맙습니다. 고맙습니다. 고맙습니다. 고맙습니다. 약간의 수정을 가해서 작동 시켰고 슈퍼 매끄러운 모습을 보였습니다. – iosfreak
@rob mayoff 문제는 내가 'UITabBar'를했을 때입니다. 키보드가'UITabBar' 아래에있는'UITextField' 슬라이드를 숨길 것입니다. 왜? –
탭 막대가있는 경우 키보드가 숨겨져있을 때 키보드의 프레임 대신 탭 막대의 프레임을 기반으로'newTextFieldFrame'을 계산해야합니다. –