2011-03-23 4 views
2

잘 알려진 whatsapp로 예제를 보여 드리겠습니다 키보드에서 텍스트를 터치하면 키보드가 튀어 나오므로 이동 막대를 이동하거나 모든 막대를 위로 이동하고보기의 크기를 절반으로 조정해야 텍스트가 계속 볼 수 있습니다 내가 버튼을 입력하고 송신있어 것을아이폰을위한 키보드를 보여줄 때 UIView의 크기를 재조정하는 방법?

1 단계 : http://www.appbank.net/wp-content/uploads/2010/10/WhatsAppMessenger-18.jpg

2 단계 : http://www.onetooneglobal.com/wp-content/uploads/2011/02/onetoone_whatsapp_2.png

무엇이를 달성하는 가장 좋은 방법이 될 것입니다?

답변

6


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

이 방법을 시도해보십시오. 귀하의 요구 사항에 따라 편집하십시오.

+0

좋아요, 당신의 코드를 사용했는데, 나는 void, IBAction 대신 put을하고 textedit에 이벤트를 할당해야했습니다. 여전히 keyboardWillHide와 keyboardWillShow의 역할을 알지 못합니다. 그들을 사용하는 방법, .., 키보드를 숨기기위한 내가 사용 http://stackoverflow.com/questions/2586937/how-to-hide-the-keyboard-programatically-in-iphone. 이 멋진 솔루션을 주셔서 감사합니다 – PartySoft

+0

textFieldDidBeginEditing 및 textFieldDidEndEditing을 사용하는 경우 애니메이션 타이밍이 완벽하지 않을 수 있으므로 keyboardWillShow 및 keyboardWillHide를 사용하여 적절하게 타이밍을 조정할 수 있습니다. 이 두 가지 방법은 조금 일찍 발생합니다. 어쨌든 귀하의 경우에는 textFieldDidBeginEditing 및 textFieldDidEndEditing 충분하다고 생각합니다. :) –

+0

stayup = YES; 여기에 머물러있는 것이 무엇입니까? –

2

당신은 UIKeyboardDidShowNotificationUIKeyboardDidHideNotification받는 듣고 싶은, 당신은 알림 센터는 의지에 귀하의 의견 크기를 조정 제공 한 선택에 대응하는 방법 (일반적으로 UIView.frame 속성을 변경하여) 것

+0

Apple은 이에 대한 훌륭한 자습서를 제공합니다. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/ uid/TP40009542-CH5-SW1 – To1ne

관련 문제