Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton's Custom 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 currently is bigger than 28 x 28 and it's actually 100 x 100.

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

like image 395
Joo Park Avatar asked May 08 '10 01:05

Joo Park


2 Answers

The image of the button will not be rescaled. Set as the backgroundImage if you need rescaling.

[btnCancel setBackgroundImage:cancelImg forState:UIControlStateNormal];
like image 178
kennytm Avatar answered Sep 20 '22 19:09

kennytm


This behaviour must have changed since, because the above code produces a scaled down button on iOS 4.2.

like image 32
Teo Sartori Avatar answered Sep 21 '22 19:09

Teo Sartori