Search code examples
iossizeframetransformautoresize

IOS - Autoresizing views issue when shrinking too much


I have my subviews inside my view with the appropriate AutoresizingMasks so that they accommodate accordingly when the view's size changes.

My issue comes when the view is shrinked too much, or even not that much. The subviews seems to forget their original position and begin to place in weird positions or scale too much or too less.

The original requirement is to "Shrink a view" to its center and then to "Popit up" like a balloon, from the ~=zero-size to the original frame size.

I have shrinked and popped up views before without much problem in other projects I don't know if it's because the view is more complex now


Solution

  • Well, as I said, I have done this before so I just checked my other project and I found my problem, I post the answer here to share the knowledge for anyone interested, as I couldn't google this moments before.

    The problem is that shrinking or growing views using their frame is not the correct method to do this.

    Bad example:

    [UIView animateWithDuration: 0.4 delay:0 options: UIViewAnimationCurveEaseInOut animations:^{
        self.frame = CGRectMake(frame.origin.x + frame.size.width/2, frame.origin.y + frame.size.height/2, 10, 10);
    
    } completion: nil];
    

    instead, transforms should be used:

        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    
        [UIView animateWithDuration: 0.4 delay:0 options: UIViewAnimationCurveEaseInOut animations:^{
    
           self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
    
       } completion: nil];
    

    The above places the elements where they are supposed to after I shrink and grow my views, taking into account that is is only for animation purposes.

    Hope it helps.