ios UIButton 不听内容模式设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2950613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:18:37  来源:igfitidea点击:

UIButton doesn't listen to content mode setting?

iosuibuttoncontentmode

提问by Dan Ray

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

firstButton 是自定义类型的 UIButton。我以编程方式将其中三个放在表格的每个单元格中,因此:

[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.

在其他地方,我告诉它clipToBounds。我得到的是图像中心正方形的裁剪,而不是它的纵横比例渲染。我已经尝试了很多方法,包括在 firstButton.imageView 上设置 mode 属性,这似乎也不起作用。

回答by Dave

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.

我花了一些搜索和试验,但我找到了解决方案。只需设置 UIButton 内的“隐藏”ImageView 的 ContentMode。

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

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

也许这就是丹雷在他接受的答案中暗指的,但我怀疑不是。

回答by Alfie Hanssen

If you're dealing with the UIButton's image (as opposed to it's backgroundImage), setting the contentMode on the UIButton itself or on its imageView has no effect (despite what other answers say).

如果您正在处理 UIButton 的图像(而不是它的 backgroundImage),则在 UIButton 本身或其 imageView 上设置 contentMode 没有效果(尽管其他答案是这样说的)。

Alternatively do this instead:

或者这样做:

self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

Or size your image accordingly.

或相应地调整图像大小。

OR just use a UIImageView (which properly respects contentMode) with a UITapGestureRecognizer attached to it, or a transparent UIButton on top of it.

或者只使用一个 UIImageView(它正确地尊重 contentMode)和一个 UITapGestureRecognizer 附加到它,或者一个透明的 UIButton 在它上面。

回答by Chris Nolet

Rather than setting the contentModeon the button itself, you'll want to set contentHorizontalAlignmentand contentVerticalAlignmentproperties and (crucially) the contentModefor the button's imageViewfor any kind of aspect fill or fit. Here's an example:

而不是contentMode在按钮本身上设置,您需要设置contentHorizontalAlignmentcontentVerticalAlignment属性和(至关重要的)contentMode按钮的imageView任何类型的方面填充或适合。下面是一个例子:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.imageView.contentMode = UIViewContentModeScaleAspectFit;

Of course, you can also do other things like aligning the button's image to the top of the button. If you don't need an aspect fill or fit, you just can set the alignment by itself:

当然,您还可以执行其他操作,例如将按钮的图像与按钮顶部对齐。如果您不需要方面填充或适合,您可以自行设置对齐方式:

button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

In Swift, you'll want to use:

在 Swift 中,您需要使用:

button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .scaleAspectFit

This works for all versions of iOS, including the latest versions with auto-layout, (i.e. iOS 4 up to iOS 11+).

这适用于所有版本的 iOS,包括具有自动布局的最新版本(即 iOS 4 到 iOS 11+)。

回答by Pierre

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.

经过几个小时的困惑,以下是我如何让它在 iOS 3.2 下工作。正如 dusker 所提到的,使用 setBackgroundImage 而不是 setImage 为我完成了这项工作。

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];

回答by Dan Ray

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.

答案是使用 UIImageView 和你想要的所有可爱的内容模式设置,然后在它上面放置一个自定义按钮。愚蠢的是你不能一次完成所有的事情,但看起来你不能。

回答by ninjaneer

My answer is similar to Kuba's. I needed my image to be programatically set.

我的答案与库巴的相似。我需要以编程方式设置我的图像。

UIImage *image = [[UIImage alloc] initWithContentsOfFile:...];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill; //this is needed for some reason, won't work without it.
for(UIView *view in button.subviews) {
    view.contentMode = UIViewContentModeScaleAspectFill;
}

回答by iOSDevSF

Found a fix for this. Set the adjustsImageWhenHighlightedproperty of UIButtonto NO.

找到了解决方法。将 的adjustsImageWhenHighlighted属性设置UIButtonNO

  UIButton *b = [[UIButton alloc] initWithFrame:rect];
        [b setImage:image forState:UIControlStateNormal];
        [b.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [b setAdjustsImageWhenHighlighted:NO];

Hope this helps. Feel free to comment below, I will follow up on any questions that you have.

希望这可以帮助。欢迎在下方留言,有任何问题我都会跟进。

回答by Tariq

Only solution which worked for me:

唯一对我有用的解决方案:

[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

回答by Vaibhav Saran

Swift 3

斯威夫特 3

self.firstButton.imageView?.contentMode = .scaleAspectFill

回答by JohnPayne

I believe we have a simple interface builder issue here - apparently the IB ignores any content-mode changes AFTER you have set the image-property.

我相信我们在这里有一个简单的界面构建器问题 - 显然 IB 在您设置图像属性后会忽略任何内容模式更改。

the solution is as simple: set the content mode, remove previously set image-names (make sure you remove it in all states, default, highlighted etc.), then re-enter the desired image-names in all desired states - et voilà.

解决方案很简单:设置内容模式,删除之前设置的图像名称(确保在所有状态、默认、突出显示等中将其删除),然后在所有所需状态中重新输入所需的图像名称 - 等等.