Search code examples
iosobjective-canimationuiviewuiwindow

Wrong frame after adding transformed view to UIWindow or UIView


The following code will break the frame (which will be enormous), as seen in iOS 8.1.3.

someView.layer.transform = CGAffineTransformMakeScale(0.001f, 0.001f);
[[[[UIApplication sharedApplication] keyWindow].subviews lastObject] addSubview:someView];

[UIView animateWithDuration:0.3f animations:^{
    someView.transform = CATransform3DIdentity;
}];

Removing the transformation and replacing it with a simple frame movement animation works well. Why?


Solution

  • After some investigation and reasoning I came to the conclusion that it might be a good idea to apply the transformation of someView after it has been added to a view. And this turned out to be the solution. So we write:

    [[[[UIApplication sharedApplication] keyWindow].subviews lastObject] addSubview:someView];
    someView.layer.transform = CGAffineTransformMakeScale(0.001f, 0.001f);
    
    [UIView animateWithDuration:0.3f animations:^{
        someView.transform = CGAffineTransformIdentity;
    }];
    

    Which works as expected.