Michael
Michael

Reputation: 51

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];

Upvotes: 5

Views: 1552

Answers (2)

eleven_huang
eleven_huang

Reputation: 87

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;

Upvotes: 2

Allyn
Allyn

Reputation: 20441

Don't set the frame of cookbookViewButton - set the bounds.

Upvotes: 0

Related Questions