2013-04-02 3 views
2

UITextField 및 UITextViews가 포함 된 UIScrollView가 있습니다. UIKeyboardDidChangeFrameNotification에 등록했습니다. 내가 텍스트 필드 나 텍스트 뷰를 탭하면, 변화 프레임 알림 작업을 트리거했고,키보드 프레젠테이션에서 UITextView 또는 UITextField를 탭하면 UIScrollView 문제가 발생합니다.

- (void)keyboardDidChangeFrame:(NSNotification *)notification 
{ 
    CGRect keyboardEndFrame; 
    [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGRect intersection; 
    UIView *theFirstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder]; 

    if ([theFirstResponder isKindOfClass:[UITextView class]]) { 
     if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) { 
      keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 416, keyboardEndFrame.size.height, keyboardEndFrame.size.width); 
     } 
     else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
     { 
      keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 0, keyboardEndFrame.size.height, keyboardEndFrame.size.width); 
     } 
    } 
    else 
     keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, keyboardEndFrame.origin.x, keyboardEndFrame.size.height, keyboardEndFrame.size.width); 

    screenRect = CGRectMake(screenRect.origin.y, screenRect.origin.x, screenRect.size.height, screenRect.size.width); 
    if(CGRectEqualToRect(lastKBDRect, keyboardEndFrame)) { 
     return; 
    }                                                                                                                                                                                                                                                                                                                                                                      
    lastKBDRect = keyboardEndFrame; 
    if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) { 
     // Keyboard is visible 

     //Convert Frame of the first responder, in context of the view that needs to be shifted 
     UIView *firstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder]; 
     CGRect theRect = [firstResponder convertRect:firstResponder.frame toView:[UIApplication sharedApplication].keyWindow]; 
     theRect = CGRectMake(theRect.origin.y, theRect.origin.x > 768 ? 750 : theRect.origin.x, theRect.size.height, theRect.size.width); 

     intersection = CGRectIntersection(keyboardEndFrame, theRect); 

     //If intersection is null, then no need to shift anything. Simply return. 
     if(CGRectIsNull(intersection)) { 
      return; 
     } 
     //Shift the view so that the first responder view is completely visible, keeping the constraint that the origin of the first responder view is also visible. 


     //Remember the current offset, so when we shift the view back, we shift it to the proper position. 
     if (!wasContentViewShifted) { 
      lastContentOffset = contentScrollView.contentOffset; 
      lastContentSize = contentScrollView.contentSize; 
      wasContentViewShifted = YES; 
     } 

     CGFloat offset = theRect.origin.y + theRect.size.height - keyboardEndFrame.origin.y; 
     if((theRect.origin.y - offset) < 40) { 
      offset += 42; 
     } 
     [UIView animateWithDuration:0.3f animations:^{ 
     contentScrollView.contentOffset = CGPointMake(0, contentScrollView.contentOffset.y + offset); 
      contentScrollView.contentSize = CGSizeMake(0, lastContentSize.height + (600 - theRect.size.height)); 
    }]; 

    } else { 
     // Keyboard is hidden. Move the view back only if it was shifted. 
     if(wasContentViewShifted) { 
      wasContentViewShifted = NO; 
      [UIView animateWithDuration:0.3f animations:^{ 
       contentScrollView.contentOffset = lastContentOffset; 
       contentScrollView.contentSize = lastContentSize; 
      }]; 
     } 
    } 
} 

응용 프로그램은 가로 방향을 지원 아래와 같이 나는 스크롤 뷰의 contentOffset를 조정합니다. 내가 여기에 직면 해요 문제가

  1. 텍스트 뷰에 태핑

    키보드를 제시하고,이 키보드가 숨겨져 있으면 텍스트 뷰는 상단에 스크롤입니다. 이제 방위 (가로를 왼쪽에서 오른쪽으로 가로 방향)를 변경하면 textView 스크롤이 맨 위로 이동하여 보이지 않게됩니다.
  2. TextView의 스크롤은 가로 방향으로는 가능하지만 가로 방향에서는 가능하지 않으며 그 반대도 마찬가지입니다. 이것은 내가 사용하고있는 keyboardEndFrame 값 때문입니다. keyboardEndFrame 원점 값을 사용해야하지 않습니까? 그렇지 않다면 대안은 무엇입니까?
  3. 때때로 textField에서는 작동하지만 textView에서는 작동하지 않습니다.

답변

1

그런 복잡한 솔루션을 사용하는 대신 this을 사용해보세요. 간단한 해결책 중 하나는 here입니다.

또한 프레임을 하드 코딩하는 것은 바람직하지 않습니다. 자세한 내용은 apple documentation을 참조하십시오.

+1

고맙습니다. 그것은 효과가 있었다. 내 코드를이 링크 (https://github.com/michaeltyson/TPKeyboardAvoiding)의 코드로 바꿨습니다. 고맙습니다. – Basavesh

+0

@Basavesh 대답이 도움이 되었다면, 그것을 수용 가능한 대답으로 표시 할 수 있습니다. – Skywalker

관련 문제