UIView动画中调整UITextView框架大小时,初始跳跃问题

4

当用户点击字段并开始编辑时,我有以下代码来扩展文本区域和它的父视图:

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text = @"";

    CGRect titleViewFrame = self.titleLabel.frame;
    titleViewFrame.size.height = kExpandedSubviewFrameHeight;

    CGRect titleTextViewFrame = self.titleTextView.frame;
    titleTextViewFrame.size.height = kExpandedSubviewFrameHeight;

    [UIView animateWithDuration:0.4f
        animations:^{
                         self.titleLabel.frame = titleViewFrame;
                         self.titleTextView.frame = titleTextViewFrame;
        }];
}

除了用户第一次点击此字段时,文本视图的y原点会稍微上移之外,一切都正常工作: enter image description here 非常感谢您提供任何帮助。

你也在调整键盘吗?如果是的话,那么你应该从willShow通知中获取时间、持续时间和大小。 - David Rönnqvist
不需要调整键盘,因为这些字段已经足够高了。谢谢。 - fogwolf
您可能只想对titleView框架进行动画处理,而不是textView框架(因为没有边框)。在动画完成块中设置textView框架。 - n00bProgrammer
1个回答

1
有点晚了,但我刚遇到同样的问题,当调整uitextview的大小时,我认为这与文本视图的滚动有关。这是我的代码,在动画高度时没有任何反弹。我使用NSLayoutConstraint来适应我的高度。
- (void)textViewDidChange:(UITextView *)textView
{
    CGSize size = [messageView sizeThatFits:CGSizeMake(messageView.frame.size.width, FLT_MAX)];
    [UIView animateWithDuration:0.4f
                     animations:^{
                         messageHeightConstraint.constant = size.height;
                         [self.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished) {
                         [messageView scrollRangeToVisible:NSMakeRange([messageView.text length], 0)];
                     }
    ];

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接