1

I want to make a ViewController like Apple's Mail App e-mail details controller. There are "From", "To", etc. When I add new e-mails, the first view changes it's size and all the other views in the stack should also change their origin and the whole container view should change it's size. Are there any beautiful solutions for this problem? Please, don't send me to Three20 Mail Composer. I have seen it already. I want to implement my own similar controller. Needed some code example.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
  • 1
    Some good discussion here: http://stackoverflow.com/questions/9045000/custom-mail-composer-just-like-three20-message-composer – Luke Aug 21 '12 at 12:13

1 Answers1

3

It's been publicly announced that iOS6 will have AutoLayout - this basically is designed to solve this kind of thing.

For earlier versions, it depends exactly what you want it to do. In simple cases, put all the views that need to shift down together into a blank container UIView. Then you can change and animate that container view instead of having to move each view individually.

If you have a lot of moving parts, that approach doesn't scale well. In those cases, I now tend to make the screen's main container view a custom subclass class (as opposed to the default plain UIView and having the layout logic in the view controller). This view subclass is responsible for working out the necessary sizes and positions on that screen in a centralised method, based on the content to be shown. That method then can be called from - layoutSubviews and in any UIView animation blocks. Having it called from - layoutSubviews means it also works with autorotation.

As for working out the positions: Basically, just work downwards from the top. For every view, calculate how high it needs to be, what the gap to its lower neighbour is, and increment the running y value by those numbers. Set the y coordinate of the next-lower view's origin to that running y value, then add its height and margin, etc. When you're done, you should be able to use the final y value as the height of the contentSize of the scroll view that presumably contains all these views.

pmdj
  • 22,018
  • 3
  • 52
  • 103