12

현재 키보드 상단에 UITextField가 있습니다. 탭하면 키보드 상단에 붙어 부드럽게 위로 움직여야합니다. 키보드의 정확한 길이와 애니메이션 유형을 알지 못하므로 정말 울퉁불퉁합니다. 누군가가 전에이 같은 짓을 한 경우becomeFirstResponder 또는 resignFirstResponder 이벤트가 발생하면 키보드 상단에 객체를 유지 하시겠습니까?

[theTextView resignFirstResponder]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
// Frame changes go here (move down 216px) 
[UIView commitAnimations]; 

[theTextView becomeFirstResponder]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
// Frame changes go here (move up 216px) 
[UIView commitAnimations]; 

, 당신이 애니메이션이 원활하고는 바의 상단에 "붙어"입니다 것처럼 보이게하는 데 사용되는 설정을 알고 싶습니다 : 여기에 내가 가진 무엇 키보드.

답변

44

UIKit은 키보드를 표시 할 때 UIKeyboardWillShowNotification을 게시하고 키보드를 표시하면 UIKeyboardWillHideNotification을 표시합니다. 이 알림에는 UITextField을 올바르게 애니메이션하는 데 필요한 모든 것이 포함되어 있습니다. UITextFieldmyTextField이라는 속성에 있다고 가정 해 보겠습니다.

먼저 알림을 등록해야합니다. 등록 할 곳은 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.framemyTextField.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]; 
} 
+1

고맙습니다. 고맙습니다. 고맙습니다. 고맙습니다. 약간의 수정을 가해서 작동 시켰고 슈퍼 매끄러운 모습을 보였습니다. – iosfreak

+0

@rob mayoff 문제는 내가 'UITabBar'를했을 때입니다. 키보드가'UITabBar' 아래에있는'UITextField' 슬라이드를 숨길 것입니다. 왜? –

+0

탭 막대가있는 경우 키보드가 숨겨져있을 때 키보드의 프레임 대신 탭 막대의 프레임을 기반으로'newTextFieldFrame'을 계산해야합니다. –

0

키보드로 고정하려면 UITextField를 만들기 위해 오프셋 계산을 수행하고 setContentOffset:animation: 메서드를 사용하여 스크롤 뷰 (UITextField가 UIScrollView에 배치되었다고 가정)에 오프셋 변경 사항을 적용해야합니다.

+0

ScrollView에 없습니다. scrollView는 존재하지도 않으며, 그것은 UITableView입니다. – iosfreak

+2

@ phpnerd211 : UITableView는 UIScrollView의 하위 클래스입니다. – Andrew

5

설정 텍스트 필드 (또는 텍스트 필드를 잡고보기) 필드의 inputAccessoryView로 그 너는 편집 중이다. 그런 다음 키보드 상단에 자동으로 연결되어 적절하게 애니메이션이 적용됩니다.