views:

502

answers:

5

firstButton is a UIButton of type Custom. I'm programmatically putting three of them across each cell of a table, thusly:

[firstButton setImage:markImage forState:UIControlStateNormal];
[firstButton setContentMode:UIViewContentModeScaleAspectFit];
[cell.contentView addSubview:firstButton];

Elsewhere, I'm telling it to clipToBounds. What I get is a crop of the center square of the image, rather than an aspect-scaled rendering of it. I've tried this lots of ways, including setting the mode property on firstButton.imageView, which also doesn't seem to work.

A: 

Instead of setImage try setBackgroundImage

dusker
Hmp. A, that's dumb, it should respect the CONTENT MODE setting for its CONTENT, right? And B, it STILL doesn't seem to respect the mode setting--it's now locked to "stretch" (which is, in this case, more like "squeeze"), and isn't scaling the content with respect to its aspect ratio.
Dan Ray
A: 

The answer is to use a UIImageView with all the lovely Content Mode settings you want, and then layer a custom button on top of it. Dumb that you can't do that all in one shot, but it appears that you can't.

Dan Ray
+1  A: 

I had the same problem. I see this question is a little old, but I want to provide a clear and correct answer to save other folks (like me) some time when it pops up in their search results.

It took me a bit of searching and experimenting, but I found the solution. Simply set the ContentMode of the "hidden" ImageView that is inside the UIButton.

[[firstButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[firstButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

Perhaps that's what Dan Ray was eluding too in his accepted answer, but I suspect not.

Dave
No, it wasn't. The solution I settled on was to use a UIImageView to hold the image, then a clear, transparant UIButton of type "custom" on top of it.
Dan Ray
Dave, on iOS 3.2 at least, your solution doesn't seem to work for me. Too bad, because I was hoping to get the highlighting of the image on click "for free". I even made sure that button.imageView wasn't nil where I was setting contentMode, and set the image after setting contentMode, just like you're doing.
Jacques
Just to clarify, "doesn't work" means "the image is shown at its full resolution, not scaled down as I was hoping".
Jacques
Jacques, the above works for me in iOS 4.x. Unfortunately, I can reproduce the "won't resize" issue when building with a v3.2 target. I tried changing the UIButton view's frame size, but that didn't seem to help either. ugh.
Dave
A: 

After a couple of hours of confusion, here's how I got it to work under iOS 3.2. As dusker mentioned, using setBackgroundImage instead of setImage did the job for me.

CGRect myButtonFrame = CGRectMake(0, 0, 250, 250);
UIImage *myButtonImage = [UIImage imageNamed:@"buttonImage"];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[myButton setBackgroundImage:myButtonImage forState:UIControlStateNormal];
[myButton setFrame: myButtonFrame];
[myButton setContentMode: UIViewContentModeScaleAspectFit];
Pierre
A: 

I also advice to have a look at the adjustsImageWhenHighlighted UIButton property to avoid weird deformations of the image, when the button is pressed.

Mathieu Godart