Search code examples
objective-cios6autolayout

Moving elements when changing UIView frame


I have few UITextView's. Every TextView changes it's sizes on focus - it grows about 50px with animation like this:

[UITextView animateWithDuration:.1f animations:^{
    CGRect new_frame = textView.frame;
    new_frame.size.height += height;
    textView.frame = new_frame;
}];

Now my problem is I need to move TextViews under the focused one 50px down (also with animation apparently). I'm wondering how to do that the simplest way. My app is iOS6 only, so I can use autolayouts. I tried and failed. I tried using struts and springs - also failed.

The only solution that comes to mind is to just move every element that 50 px down with animation. But it sounds cheap to me. Any ideas how to do it? Is it possible with autolayouts?

EDIT

Here are the layouts I've been trying:

Vertical layout

enter image description here

Here is how it looks in simulator after clicking the first TextView:

enter image description here

So as you can see it rather cover's next view and doesn't keep the constraint.


Solution

  • Don't combine frames with autolayout, that'll give you unpredictable results.

    Remove the constraint from the top of the second UITextView to the top of its superview.

    Then create an outlet for the height constraint of the UITextView.

    Use the following code to adjust the height of the UITextView:

    - (void)textViewDidBeginEditing:(UITextView *)textView
    {
        [UITextView animateWithDuration:.1f animations:^{
            self.textViewHeightConstraint.constant +=50;
            [self.view layoutIfNeeded];
        }];
    }
    
    -(void)textViewDidEndEditing:(UITextView *)textView
    {
        [UITextView animateWithDuration:.1f animations:^{
            self.textViewHeightConstraint.constant -=50;
            [self.view layoutIfNeeded];
        }];
    }