1

In my iOS 7 project (using XCode 5) I define a custom MyButton class which derives from UIButton and sets the background image in the drawRect method:

- (void)drawRect:(CGRect)rect
{
    [self setBackgroundImage:[UIImage imageNamed:@"redButton.png"] forState:UIControlStateNormal];
}

My main view controller contains a view with an instance of MyButton (specified in IB) with a width and height of 100.

In the viewDidLoad method I am applying a rotation transform:

self.myButton.transform = CGAffineTransformMakeRotation( 45.0/180*M_PI );

which correctly rotates the button by 45 degrees. However, it also shrinks the button so that the button fits within the original 100x100 box!

What do I need to do either to the MyButton class (preferred) or the owning view so that the size of the button is not diminished?

I tried setting the clipToBounds property on both the MyButton class as well as the view but to no avail.

Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66
  • A screenshot would help. Also the `drawRect:` code. – Wain Oct 08 '13 at 22:40
  • You're probably using Auto Layout. When a view rotates, your constraints are still specifying the view's alignment rectangle (similar to the view's frame), meaning the circumscribed rectangle around your view will not change (so the transformed view must shrink a bit). To avoid this problem, there are a few workarounds you can do, see this post here: http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used – smileyborg Oct 08 '13 at 22:41

0 Answers0