2013-04-11 1 views
0

UITextViewNSLayoutConstraint을 사용하여 키보드를 피할 수 있습니다. 다음은 제약 조건입니다.UITextView에서 NSLayoutConstraint를 사용하면 contentSize가 {0,0}으로 재설정됩니다.

키보드가 표시되거나 숨겨지면 제약 조건을 키보드 높이로 설정하여 제약 조건을 활성화합니다. 그러나 어떤 이유로 든 그렇게하면 contentSize가 {0,0}으로 재설정되므로 스크롤이 끊어집니다. handleKeyboardDidHide:에 해킹을 추가하여 contentSize를 재설정하기 전에 다시 설정했지만, 스크롤 위치가 재설정되고 입력이 시작될 때까지 커서 위치로 스크롤되지 않는 것과 같은보기 흉한 부작용이 있습니다.

- (void) handleKeyboardDidShow:(NSNotification *)notification 
{ 
    CGFloat height = [KeyboardObserver sharedInstance].keyboardFrame.size.height; 
    self.textView.constant = -height; 
    [self.view layoutIfNeeded]; 
} 

- (void) handleKeyboardDidHide:(NSNotification *)notification 
{ 
    // for some reason, setting the bottom constraint resets the contentSize to {0,0}... 
    // so let's save it before and reset it after. 
    // HACK 
    CGSize size = self.textView.contentSize; 
    self.textView.constant = 0.0; 
    [self.view layoutIfNeeded]; 
    self.textView.contentSize = size; 
} 

누구나이 문제를 완전히 피하는 방법을 알고 계십니까?

+0

그것은 당신이뿐만 아니라 슈퍼 뷰의 상단에 상단에서 제약 조건이 어쩌면 같은 소리 . 그렇다면, 당신은 그것을 없애고 싶지만 텍스트 뷰를 위해 명시 적으로 높이를 설정해야합니다. – rdelmar

+0

@rdelmar 왜 맨 위 제약 조건이 어떤 식 으로든 contentSize에 영향을 주거나 영향을 미칩니 까? 어쨌든, 상위 제약 조건을 제거하는 것은이 뷰 컨트롤러에 대해 자동 레이아웃을 모두 버리는 것을 의미합니다. – memmons

+0

상단 구속 조건이 있고 상단을 위 아래로 이동하면 구속 조건을 충족시키는 유일한 방법은 높이가 0입니다. – rdelmar

답변

1

코드에 무엇이 잘못되었는지 알지 못합니다. 원하는 경우 자세히 처리 할 수 ​​있습니다. 그러나 초기 제안으로, 가능한 경우 UITextView의 크기를 조정하지 않습니다 : 그냥이 같은 내용 스크롤 세트를 변경 :

- (void) keyboardShow: (NSNotification*) n { 
    NSDictionary* d = [n userInfo]; 
    CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    self.tv.contentInset = UIEdgeInsetsMake(0,0,r.size.height,0); 
    self.tv.scrollIndicatorInsets = UIEdgeInsetsMake(0,0,r.size.height,0); 
} 

을 그럼에도 불구하고, 나는 당신이 키보드 숨기기 애니메이션이 완료 될 때까지 기다려야 것을 발견 이러한 값을 재설정하기 전에 :

- (void) keyboardHide: (NSNotification*) n { 
    NSDictionary* d = [n userInfo]; 
    NSNumber* curve = d[UIKeyboardAnimationCurveUserInfoKey]; 
    NSNumber* duration = d[UIKeyboardAnimationDurationUserInfoKey]; 
    [UIView animateWithDuration:duration.floatValue delay:0 
         options:curve.integerValue << 16 
        animations: 
    ^{ 
     [self.tv setContentOffset:CGPointZero]; 
    } completion:^(BOOL finished) { 
     self.tv.contentInset = UIEdgeInsetsZero; 
     self.tv.scrollIndicatorInsets = UIEdgeInsetsZero; 
    }]; 
} 

(그것은 그 트릭을 어떻게 든뿐만 아니라 코드를 도울 것이라고 할 수있다.)

관련 문제