2015-01-30 2 views
0

내 응용 프로그램에서는 UITextField에서 키보드가 나타난 후 내 ViewController의 뷰를 위쪽 방향으로 움직이거나 움직이지 않아도됩니다.iOS 7 iPad 키보드가 UITextField에 나타난 후 뷰가 위쪽으로 움직일 때의 오리엔테이션 문제

이 모든 orientation.i.e에 대한 지원해야합니다
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight합니다. enter image description here

을하지만 UIInterfaceOrientationPortraitUpsideDown 및 UIInterfaceOrientationLandscapeLeft 것이를 작동하지 않습니다 : 아래 그림과 같이 UIInterfaceOrientationPortrait를 들어

내 코드는 잘 작동한다. 스크린 아래 참조주세요 UIInterfaceOrientationLandscapeLeft 뷰

상방 측 대신 오른쪽을 향해 이동한다.

enter image description here 아래쪽 대신 측 상방을 향해 UIInterfaceOrientationPortraitUpsideDown 뷰 이동을위한

.

enter image description here

이 내가 아이폰 OS 8에서이 문제를 확인했습니다 아이폰 OS 7 만 문제가

, 그것은 잘 작동합니다.

나는 키보드 모양에 UIView의 애니메이션에 대한 코드 아래 사용했다 :

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352; 

    -(void) textFieldDidBeginEditing:(UITextField *)textField 
    { 
     CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 

     CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 

     CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 

     CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
     CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
     CGFloat heightFraction = numerator/denominator; 

     if (heightFraction < 0.0) 
     { 
      heightFraction = 0.0; 
     } 
     else if (heightFraction > 1.0) 
     { 
      heightFraction = 1.0; 
     } 

     UIInterfaceOrientation orientation = 
     [[UIApplication sharedApplication] statusBarOrientation]; 
     if (orientation == UIInterfaceOrientationPortrait || 
      orientation == UIInterfaceOrientationPortraitUpsideDown) 
     { 
      animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
     } 
     else 
     { 
      animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
     } 

     if(self.view.frame.origin.y != 0.000000) 
     { 
      self.view.frame = CGRectMake(self.view.frame.origin.x,0.0,self.view.frame.size.width,self.view.frame.size.height); 
     } 

     CGRect viewFrame = self.view.frame; 
     viewFrame.origin.y -= animatedDistance; 

     NSLog(@"View frame y pos did start: %f ",animatedDistance); 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

     [self.view setFrame:viewFrame]; 

     [UIView commitAnimations]; 
    } 

    -(void) textFieldDidEndEditing:(UITextField *)textField 
    { 
     CGRect viewFrame = self.view.frame; 
     viewFrame.origin.y += animatedDistance; 

     NSLog(@"View frame y pos did end: %f ",animatedDistance); 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

     [self.view setFrame:viewFrame]; 

     [UIView commitAnimations]; 
    } 

감사

답변

1

꿀꺽합니다. 몇 가지 제안을 할 수 있습니까?

먼저 textFieldDidBeginEditing 이벤트 대신 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification에 이벤트를 첨부합니다.

내 iPad에서 앱을 실행했지만 Bluetooth 키보드가 연결되어 있다고 가정합니다. 나는 텍스트 상자에서 탭할 것이고, UIView은 위로 움직일 것이다.하지만 iPad는 내가 필요로하지 않는다는 것을 알고 있기 때문에 화면상의 키보드를 표시하지 않을 것이다.

다른 장점은 현재 화면 키보드에 하드 코딩 된 높이를 사용하고 있다는 것입니다. 우, 이건 좋은 생각이 아니야.

이 좋은 생각이 아닌 이유 중 하나 (! 한) 예를 제공하기 위해 본 게시물에서 내 스크린 샷에서보세요 :

iPad keyboard height

당신이 UIKeyboardWillShowNotification 경로를 아래로 이동하도록 선택하는 경우 , 키보드의 정확한 높이를 측정 할 수 있습니다 ...여기

-(void)onKeyboardWillShow:(NSNotification*)notification 
{ 
    // The user has turned on the onscreen keyboard. 
    // 
    NSDictionary *info = [notification userInfo]; 
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardFrame = [kbFrame CGRectValue]; 
    keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil]; 

    CGFloat keyboardHeight = keyboardFrame.size.height; 
} 

는 컨트롤의 크기를 조정하는 것이 (거의) 전체 화면을 채울 수 있도록, 그리고 때 장치의 방향 & 화면 키보드의 가시성이 변경 크기를 조정하는 방법을 보여줍니다 나는 노크 한 샘플 Xcode 프로젝트,이다 :

OnScreenKeyboardTest XCode 6.1 project

당신은 화면 키보드가 사라 만드는 "해제"버튼을 가지고 있음을 볼 수 있습니다. 이 코드는 모든 iPhone에서 실행됩니다 (&).

enter image description here

휴! 희망이 도움이됩니다.

+0

답장을 보내 주셔서 감사합니다. 당신의 솔루션이 정말로 도움이되었습니다. –

+0

쿨! 이 XCode 지옥으로 발견 한 한 가지 ... 작동 예제를 볼 때 삶이 훨씬 쉬워집니다! 기쁜 데 도움이되었습니다. –

관련 문제