2012-10-03 2 views
1

15 개의 텍스트 필드가있는 앱을 개발 중입니다. 그래서 UIScrollView를 사용하여 스크롤링을 가능하게 만들었습니다. 그러나 마지막 5 개의 텍스트 필드는 편집 할 텍스트 필드를 클릭 할 때 키보드 뒤에 숨어 있습니다. 편집 모드에있을 때 키보드 위의 텍스트 필드를 이동하는 방법은 무엇입니까? 다시 말하지만, 텍스트 필드는 UIScrollView에 있습니다. UIView가 아닙니다.키보드가 UIScrollView의 텍스트 필드를 숨기고 있습니다. 키보드에 따라 텍스트 필드를 이동하는 방법?

+0

아무 것도 UIScrollView에 대해 작동하지 않습니다. 그 이유는 질문을 게시했습니다. [키보드가있을 때 UITextField를 위로 이동하는 방법]의 가능한 복제본 – dcprog

+0

(http://stackoverflow.com/questions/1126726/how-to-make- a-uitextfield-move-up-when-keyboard-is-present) –

답변

0

scrollView에 contentSize 및 contentOffset을 적절히 설정해야합니다. 따라서 마지막 텍스트 상자가 (0,300)의 원점에있는 경우 contentSize가 CGSizeMake (0, 600) 정도가되도록 설정 한 다음 contentOffset을 (0, 250)으로 설정해야합니다.

+0

코드를 알고 있을까요? 내 말은, 지금까지 CGSizeMake를 모두 시도했다. – dcprog

2

시도 TPKeyboardAvoidingScrollView.

이미 가지고있는 스크롤보기를 잡고 클래스가 TPKeyboardAvoidingScrollView으로 변경되면 키보드가 팝업 될 때 크기와 오프셋이 조정되어 필드가 올바르게 표시됩니다.

복잡한 레이아웃 (많은 텍스트 필드 및 기타 구성 요소)이있는 경우 손으로 처리하는 것이 까다로울 수 있으므로이 방법을 사용하는 것이 좋습니다. 이 컨트롤은 매력처럼 작동하고 작동하는 것처럼 간단합니다!

0

당신은 당신의 scrollViewYtextFieldShouldBeginEditing:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if(textField == textField11 || textField == textField12 || textField == textField13 || textField == textField14 || textField == textField15) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     //set Y according to keyBoard height 
     [scrollView setFrame:CGRectMake(0.0,-220.0,320.0,460.0)]; 
     [UIView commitAnimations]; 
    } 
} 

에서 조정하고 그것이 당신이 키보드 여기

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [scrollView setFrame:CGRectMake(0.0,0.0,320.0,460.0)]; 
    [UIView commitAnimations]; 
} 
+0

if (textField == textField11 || textField == textField12 || textField == textField13)? – dcprog

+0

이것들은 당신의 마지막 5 개의'textField'입니다. 그래서 15라고 생각합니다. 11,12,13,14,15입니다. – TheTiger

+0

아니요 ... 작동하지 않습니다. – dcprog

1

반환 키를 누를 때 scrollView 프레임을 설정 줄일 수는 몇 가지 예제 코드입니다 :

#define kOFFSET_FOR_KEYBOARD 80.0 

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 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. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillHide) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 
관련 문제