0

So I had a UITableView inside a View Controller (not a UITableViewController) and to support iphone 6 & 6 Plus I started to implement Autolayout. Everything working nice and smooth right now, the problem is that I had resize animations on UIViews inside each cell of that UITableView and now animations don't resize the inner views, the constraints seem to affect everything inside the UITableView even though I only wanted to keep the UITableView centered through different devices...

The constraints I'm using on the UITableView are:

  • Center X Alignment on content view
  • Vertical Space (To the top of the content view, with a fixed value = 114)
  • Vertical Space (To the bottom of the content view, with a fixed value = 0)
  • Fixed Width on the UITableView (320)

So basically, if we're on iphones 4-5s we have a tableview using the complete width of the screen, meanwhile iphones 6 & 6 Plus have a centered tableview of 320 fixed width.

This is the first time I've used AutoLayout, so I don't know how these constraints are affecting the animations on views inside cells of the UITableView, this is how I made the call:

//before the call
barraActual.frame = CGRectMake(20, 24, 5, 30);
barraPromedio.frame = CGRectMake(295, 24, 5, 30);

//animate
[UIView animateWithDuration:1.5 animations:^{
     barraActual.frame = CGRectMake(20, 24, periodoActual, 30);
     barraPromedio.frame = CGRectMake(20+periodoActual, 24, periodoAnterior, 30);
}];

EDIT: Please note that the views (barraActual & barraPromedio) are UIViews within UITableViewCell, and this code is inside the "cellForRowAtIndexPath".

How do I stop the constraints from blocking the resize on my views? (because I've tried setting the frame without the animation call and didn't work either)

Or how to I make a animation call that works along these constraints?

Thanks!

Fdo
  • 1,053
  • 4
  • 15
  • 38
  • Using auto layout, you never change the frame property by yourself, since this will be overwritten by autolayout. You only had to specify the layout using constraints. – Reinhard Männer Sep 24 '14 at 19:26
  • So, AutoLayout is incompatible with animations? I have two UIViews (colored) and I animate them proportionally to data (that's why they are in a tableviewcell, because they are never the same) so the users gets to perceive percentages graphically (nicer to the eye) – Fdo Sep 24 '14 at 19:31
  • The constraints are targeting the UITableView, the UIVIews I'm trying to animate are inside a UITableViewCell (inside the UITableView) – Fdo Sep 24 '14 at 19:33
  • 1
    No, not at all. You simply don't have to set the frame property (and others) by yourself. You have to express your layout using constraints (e.g. distance to the container, width, height). If you do, changes in the screen size are handled automatically. BTW constraints are objects, and can be IBOutlets, so you can change them dynamically, e.g. in an animation. – Reinhard Männer Sep 24 '14 at 19:35
  • Ok, I'm still trying to understand the techniques of AutoLayout... Is there a way to get and change a constraint of a UIView instead of a IBOutlet? Because in the 'cellForRowAtIndexPath' I only work on the views accessing them with the method [cell viewWithTag:X] – Fdo Sep 24 '14 at 19:48
  • Found the array that seems to contain the constraints of a View... I'm gonna keep looking, thank you for your suggestions so far :) – Fdo Sep 24 '14 at 19:54
  • Yes. Just use the UIView's method addConstraint:, see – Reinhard Männer Sep 24 '14 at 19:55

1 Answers1

0

Thanks to Reinhard Männer for guiding me through, for whoever who might find himself with the same misconception I had:

Changing the frame of a view while using AutoLayout will make no effect (with or without animations). To make changes you will need to set new constraints or change the existing ones (most likely programmatically).

I created new constraints in Interface Builder to manage the views I wanted to animate and now my animation block looks like this:

//animate
[UIView animateWithDuration:1.5 animations:^{
    ((NSLayoutConstraint*)(barraActual.constraints[1])).constant = periodoActual;
    ((NSLayoutConstraint*)(barraPromedio.constraints[0])).constant = periodoAnterior;
}];

Good night :)

Fdo
  • 1,053
  • 4
  • 15
  • 38