【问题标题】:UITextView hidding text while typingUITextView在输入时隐藏文本
【发布时间】:2011-02-25 16:33:28
【问题描述】:

我看到一些非常奇怪的行为。我在一个视图中显示了一个文本视图,该视图是滚动视图的一部分。如果当我开始在文本视图中输入时,键盘通常会隐藏该文本视图,那么我会为文本视图设置动画并将其推到键盘上方以使其保持可见。如果我这样做,我键入的第三行和后续行将不可见,直到我关闭键盘,此时我可以看到我键入的文本。如果,当我开始在文本视图中输入时,我不需要动画它来推动它(因为键盘不会隐藏它),那么我可以看到我输入的所有文本,因此我的问题只出现在文本视图是动画的。

下面是我创建文本视图的代码:

    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];      
}

我尝试在 textViewDidChange 中调用 setNeedsDisplay/setNeedsLayout,但没有成功。

有人遇到过这个问题,或者知道如何解决吗?

谢谢!

【问题讨论】:

    标签: iphone keyboard uitextview


    【解决方案1】:

    给你一些想法。首先,我认为您在动画距离计算方面工作太努力了。我通常创建一个主视图,而不是使用 UIWindow 的尺寸(不随设备旋转)。否则,正如您所发现的,您必须考虑状态栏、电话状态栏等。使用视图可以将所有这些计算减少到一个(仅补偿两个不同的键盘高度):

    if ((orientation == UIInterfaceOrientationLandscapeLeft)|| (orientation == UIInterfaceOrientationLandscapeRight)) {
        keyboardHeight = LANDSCAPE_KEYBOARD_HEIGHT;
    } else {
        keyBoardHeight = PORTRAIT_KEYBOARD_HEIGHT; 
    }
    
    CGPoint myPoint = [self.view convertPoint:textArea.frame.origin toView:self.view];
    float windowHeight = self.view.bounds.size.height;
    
    animatedDistance = myPoint.y+textArea.bounds.size.height+keyboardHeight-windowHeight;   
    
    if (animatedDistance < 0) {
        animatedDistance = 0.0; 
    }
    

    因此,使用视图的边界可以获得所需的信息。

    其次,为什么要移动textView?为什么不直接调整滚动窗口的滚动位置?

    第三,您指的是 textView 位于滚动视图内的视图内(尽管我在代码中没有看到)。您的 textView 可能被中间视图的边界裁剪,这将导致能够输入它,但无法看到结果。你能看到你的 textView 的边界吗?

    最后,作为一个美学点,animatedDistance 是一个状态变量,你应该在你把动画放回去之后,在你的 else 分支中将 animatedDistance 设置为零。这样,如果您出于某种原因两次调用 makeTextViewVisible:textView UP:false,它就不会搞砸了。

    【讨论】:

    • 您提出的观点非常有用。我喜欢调整滚动位置的第二个想法。
    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多