UIButton不听内容模式设置? [英] UIButton doesn't listen to content mode setting?
问题描述
firstButton是Custom类型的UIButton。我以编程方式将其中三个放在一个表的每个单元格中,因此:
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];
在其他地方,我告诉它要clipToBounds。我得到的是图像中心方块的裁剪,而不是它的纵横比例渲染。我已经尝试了很多方法,包括在firstButton.imageView上设置mode属性,这似乎也不起作用。
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.
推荐答案
我遇到了同样的问题。我看到这个问题有点老了,但是我想提供一个清晰正确的答案来保存其他人(比如我)在搜索结果中弹出的时候。
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.
它花了我一些搜索和实验,但我找到了解决方案。只需设置UIButton中隐藏ImageView的ContentMode。
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];
也许这就是Dan Ray在他接受的答案中提到的,但我怀疑不是。
Perhaps that's what Dan Ray was alluding to in his accepted answer, but I suspect not.
这篇关于UIButton不听内容模式设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!