Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAffineTransform: rotation on UIButton resizes button image

I've got a strange problem using UIButtons, type custom. I'm placing 4 of those buttons on a UIScrollview, rotating each button by a random angle using CGAffineTransform. Now it seems that the buttons themselves change size depending on the angle of rotation.

UIGraphicsBeginImageContext(tempCtxSize);
[cookbookImage drawInRect:CGRectMake(imgOffsetX, imgOffsetY+frmOffsetY, cookbookImage.size.width, cookbookImage.size.height)];
[cookbookFrame drawInRect:CGRectMake(0.0f, frmOffsetY, cookbookFrame.size.width, cookbookFrame.size.height)];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

…

UIButton *cookbookViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cookbookViewButton setFrame:CGRectMake(0.0f, 0.0f, combinedImage.size.width, combinedImage.size.height)];

[cookbookViewButton setBackgroundColor:[UIColor clearColor]];
[cookbookViewButton setBackgroundImage:combinedImage forState:UIControlStateNormal];

CGAffineTransform rotation = [cookbookViewButton transform];
rotation = CGAffineTransformRotate(rotation, angle); // some random angle
[cookbookViewButton setTransform:rotation];
like image 408
Michael Avatar asked Nov 05 '22 12:11

Michael


1 Answers

This is a system bug: " Important If a view’s transform property does not contain the identity transform, the frame of that view is undefined and so are the results of its autoresizing behaviors."

from: Handling Layout Changes Automatically Using Autoresizing Rules

Solution: set the parent view's autoResizeSubviews to NO.

    parentView.autoresizesSubviews = NO;
like image 83
eleven_huang Avatar answered Nov 12 '22 12:11

eleven_huang