【问题标题】:UITextView bad content offset after animating view动画视图后的 UITextView 错误内容偏移
【发布时间】:2011-11-17 13:03:32
【问题描述】:

我有一个位于视图底部的 UITextView。当用户点击它时,我需要将视图设置为 150px 的动画。我正在使用:

- (void)textViewDidBeginEditing:(UITextView *)textView{

- (void)textViewDidEndEditing:(UITextView *)textView{

做这个。

在 iOS5 上,它运行良好。但在 iOS 4.3 上,当视图动画化时,此 UITextView 中的内容会上升几个像素。

截图:http://cl.ly/1q3e0W2G1M000u1U3w1f

有人遇到过类似问题吗?

代码:

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if([textView.text isEqualToString:@"Placeholder text"]){
        textView.text = @"";
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 150.0), self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if([textView.text isEqualToString:@""]){
        textView.text = @"Placeholder text";
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 150.0), self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];    
}

【问题讨论】:

    标签: iphone ios ios4 ios5 uitextview


    【解决方案1】:

    在 textViewDidBeginEditing 方法中调用方法时以及动画完成后,您需要重置文本视图的内容插入...

    试试这个代码:

    - (void)textViewDidBeginEditing:(UITextView *)textView{
       [textView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    

    【讨论】:

    • 谢谢!你也可以使用常量UIEdgeInsetsZero
    【解决方案2】:

    我发现接受的答案导致了一些视觉上的不规则性。以下对我来说效果更好。

    textView.contentMode = UIViewContentModeTop;
    

    【讨论】:

      【解决方案3】:

      试试 UITextField 来解决这个问题。

      【讨论】:

        猜你喜欢
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多