UIButton 不听内容模式设置?

UIButton doesn#39;t listen to content mode setting?(UIButton 不听内容模式设置?)

本文介绍了UIButton 不听内容模式设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

firstButton 是自定义类型的 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 不听内容模式设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIButton 不听内容模式设置?

基础教程推荐