How do I keep images in programmatrically-created UIButtons from being scaled?
I'm creating a series of UIButtons from an NSArray of UIImages with the following:
#define BUTTONWIDTH 66
for (int i = 0; i < [imgArray count]; i++){
toolTile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonRect = CGRectMake((BUTTONWIDTH*i)+i, 0, BUTTONWIDTH, BUTTONWIDTH);
toolTile.frame = buttonRect;
toolTile.tag = i;
[toolTile addTarget:self action:@selector(toolTileClick:) forControlEvents:UIControlEventTouchUpInside];
toolTile.imageView.contentMode = UIViewContentModeCenter;
[toolTile setBackgroundImage:[imgArray objectAtIndex:i] forState:UIControlStateNormal];
[[self view] addSubview:toolTile];
}
Unfortunately, the images are being scale开发者_Python百科d (blown up) to fill the 66x66 tile. I want them to be centered at 1:1 in the tile- supposedly toolTile.imageView.contentMode = UIViewContentModeCenter
should do that. I've tried moving this line before,after the setBackgroundImage
: -no luck. What am I missing?
The culprit was using setBackgroundImage:forState:[UIControlState]
instead of setImge:forState:[UIControlState]
.
SetBackgroundImage
apparently always scales, SetImage:
honors the imageView.contentMode
.
精彩评论