Search code examples
iphoneiosuibuttoncalayerrounded-corners

Modifying UIButtons layer depending on state


I have a background UI Image which is shown whenever i touch a UIButton:

[btn setImage:[UIImage imageNamed:@"background.png"] forState:UIControlStateNormal];

I have also modified the layer of the uibutton to give a rounded corner

[btn.layer setCornerRadius:10];

This works but the corner radius is not being used for the background image. I can access the image at

[btn backgroundImageForState:UIControlEventTouchDown]

but as this is an UIImage it will not have a CA Layer

What I want is for the background image to also be supported by the CALayer or at the very least should support rounded corners.


Solution

  • I guess you need to set masksToBounds to YES:

    btn.layer.masksToBounds = YES;