我看到一些非常奇怪的行为。我有一个文本视图显示在一个视图,这是一个滚动视图的一部分。如果当我开始在文本视图中键入内容时,键盘通常会隐藏该文本视图,然后对文本视图进行动画处理并将其推到键盘上方以保持可见。如果我这样做了,我键入的第三行和后续行将不可见,直到我忽略键盘时,我才能看到我键入的文本。如果当我开始在文本视图中输入内容时,我不需要对它进行动画处理(因为键盘没有隐藏它),那么我可以看到我输入的所有文本,因此我的问题仅出现在文本视图是动画的。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