2013-03-19 2 views
-3

상단 텍스트 필드를 제외하고, 타이핑을 시작할 때 iphone 키보드가 나머지 텍스트 필드를 모두 숨 깁니다. 키보드 위에 활성 텍스트 필드를 표시 한 다음 키보드가 숨겨 졌을 때 정상 텍스트로 다시 가져 오려면 어떻게해야합니까?아이폰 키보드가 나타나면 활성 텍스트 필드를 중앙에 표시하는 방법은 무엇입니까?

+0

여기에 코드를 붙여 넣을 수 있습니까? –

+0

가상 키보드가 팝업되면 전체보기를 위로 이동하십시오. 이것을보십시오. http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically –

답변

0

보기 상단에 스크롤 뷰를 사용하고 선택한 텍스트 필드에 따라 수학적 contentOffset 설정을 구현해야합니다. 선택한 텍스트 필드는 델리게이트 메서드에서 가져올 수 있으며 오프셋 계산은 텍스트 필드 프레임

1

UIKeyboardWillShowNotificationUIKeyboardWillHideNotification으로 처리 할 수 ​​있습니다. 이러한 알림과 함께 제공된 NSNotification 개체에는 키보드가 애니메이션 처리를 마친 후 프레임에 대한 정보가 포함 된 userInfo 사전이 있습니다. 이러한 알림에 대한 응답으로 레이아웃을 조정하여 원하는 항목을 키보드로 가리지 않도록하십시오.

1

모든 텍스트 필드를 스크롤보기에 배치하기 전에 다음 코드를 사용하십시오.

#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]; 
} 
+0

너무 높으면이 코드가 textField를 숨기지 않겠습니까? – Jsdodgers

+0

코드는 텍스트 필드에서 작동하지만 바닥에있는 uitextview에서는 작동하지 않습니다. – zzzzz

+0

github에서 BSKeyboarcontrols를 사용하는 경우 예 – Vinodh

1

UITextFieldUIScrollView에 넣고 같은 UIScrollViewcontentOffSet 재산 사용

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField == txt1) // 
    { 
     [txt1 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 20); 
    } 

    else if (textField == txt2) // 
    { 
     [txt1 resignFirstResponder]; 
     [txt2 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 60); 
    } 
    //And so on.. 
    return YES; 
} 

희망을, 당신은 답을 얻을 수 있습니다.

감사합니다.

+0

'scrMainView'란 무엇입니까? 적절한 세부 사항을 언급하면 ​​사용자는 당신이 무엇을 제안하는지 알게되지 않을 것입니다. 'scrMainView'는 아마 scollview 맞습니까? –

+0

예. scrMainView 내 scrollview, 그래서 그것은 사용자가 쉽게 상호 작용할 수 있도록 조금 상한에 콘텐츠 오프셋을 설정합니다. –

+0

자세한 내용을 입력하십시오. 그냥 내 편집을 봐. 그것은 당신의 대답에 대한 명확한 생각을 줄 것입니다. –

관련 문제