dormitkon
dormitkon

Reputation: 2526

UITextView bad content offset after animating view

I have one UITextView which is on the bottom of view. When user tap in it, I need to animate view 150px up. I'm using:

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

and

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

to make this.

On iOS5, it works fine. But on iOS 4.3, when view is animated, content in this UITextView goes few pixels up.

Screenshot: http://cl.ly/1q3e0W2G1M000u1U3w1f

Someone had similar issues?

Code:

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

Upvotes: 4

Views: 3033

Answers (3)

Muhammed Ayaz
Muhammed Ayaz

Reputation: 111

Try UITextField to solve this problem.

Upvotes: -4

jin
jin

Reputation: 735

I found that the accepted answer caused some visual irregularities. The following worked much better for me.

textView.contentMode = UIViewContentModeTop;

Upvotes: 3

Pedja
Pedja

Reputation: 66

You need reset content inset of your text view when call method in textViewDidBeginEditing method and after you animation is done...

Try this code:

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

Upvotes: 4

Related Questions