Custom UIButton Image and Frame

I have the following code.

UIImage *cancelImg = [UIImage imageNamed:@"cancel.jpeg"]; UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom]; btnCancel.userInteractionEnabled = YES; [btnCancel setFrame:CGRectMake(0.0,0.0, 28.0, 28.0)]; [btnCancel setImage:cancelImg forState:UIControlStateNormal]; cell.accessoryView = btnCancel; 

cancel.jpeg is currently larger than 28 x 28, and in fact 100 x 100.

Why does the button display an image size of 100 x 100 when I set the UIButton size to 28 x 28?

+6
iphone uibutton
source share
3 answers

The image button of the button will not be changed. Set as backgroundImage if you need to scale.

 [btnCancel setBackgroundImage:cancelImg forState:UIControlStateNormal]; 
+13
source share

This behavior should have changed since then, since the code above creates a smaller button on iOS 4.2.

+3
source share

I experimented in iOS 5 with custom buttons that automatically change when the device orientation changes. I set both backgroundImage and the image with the same initial borders as the button, and what I see is that backgroundImage changes and the image is missing, but remains in the center of the button frame.

0
source share

All Articles