我看到一些非常奇怪的行爲。我有一個文本視圖顯示在一個視圖,這是一個滾動視圖的一部分。如果當我開始在文本視圖中鍵入內容時,鍵盤通常會隱藏該文本視圖,然後對文本視圖進行動畫處理並將其推到鍵盤上方以保持可見。如果我這樣做了,我鍵入的第三行和後續行將不可見,直到我忽略鍵盤時,我才能看到我鍵入的文本。如果當我開始在文本視圖中輸入內容時,我不需要對它進行動畫處理(因爲鍵盤沒有隱藏它),那麼我可以看到我輸入的所有文本,因此我的問題僅出現在文本視圖是動畫的。UITextView在輸入時隱藏文本
下面是我的代碼創建的文本視圖:
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(...)];
textView.font = [UIFont systemFontOfSize:18.0];
textView.backgroundColor = [UIColor whiteColor];
textView.userInteractionEnabled = YES;
textView.clipsToBounds = YES;
textView.delegate = self;
textView.layer.borderWidth = 1;
textView.layer.borderColor = [[UIColor blackColor] CGColor];
textView.layer.cornerRadius = 10;
[self addSubview:textView];
[textView release];
下面是動畫文本視圖代碼:
- (void) makeTextViewVisible: (UITextView *)textArea up:(BOOL) up {
if (up) {
animatedDistance = 0;
CGPoint myPoint = [textArea.superview convertPoint:textArea.frame.origin toView:[UIApplication sharedApplication].keyWindow];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
animatedDistance = PORTRAIT_KEYBOARD_HEIGHT - (950 - myPoint.y) + 150;
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
animatedDistance = PORTRAIT_KEYBOARD_HEIGHT + (PORTRAIT_KEYBOARD_HEIGHT - myPoint.y);
}
else if (orientation == UIInterfaceOrientationLandscapeLeft) {
animatedDistance = myPoint.x - LANDSCAPE_KEYBOARD_HEIGHT + 120;
}
else if (orientation == UIInterfaceOrientationLandscapeRight) {
animatedDistance = LANDSCAPE_KEYBOARD_HEIGHT - myPoint.x + 100;
}
if (animatedDistance < 0) {
animatedDistance = 0.0;
}
CGRect viewFrame = textArea.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[textArea setFrame:viewFrame];
[UIView commitAnimations];
}
else {
CGRect viewFrame = textArea.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[textArea setFrame:viewFrame];
[UIView commitAnimations];
}
我打過電話setNeedsDisplay/setNeedsLayout內textViewDidChange,但沒有運氣。
以前有人遇到過這個問題,或者知道如何解決它?
謝謝!
非常有用的觀點。我喜歡調整滾動位置的第二個想法。 – 2011-02-28 12:31:23