UIButton 不监听内容模式设置?

发布于 2024-09-04 11:23:53 字数 376 浏览 9 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

过气美图社 2024-09-11 11:23:54

我还建议查看 adjustsImageWhenHighlighted UIButton 属性,以避免按下按钮时图像出现奇怪的变形。

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

放手` 2024-09-11 11:23:54

在试图解决这个问题的过程中,随着时间的推移,我的方法变得有点黑客化,我最终将 UIButton 子类化并覆盖 setHighlighted:

对我来说,只需将图像 alpha 值降低到 0.5,因为它们在黑色上背景。

然而,只有当我注释掉 [super setHighlighted:] (看起来图像拉伸代码正在进行时)它才有效,这根本不像解决这个问题的正确方法......一切似乎都是不过工作正常。当我继续努力时,我们会看到它的效果如何。

- (void)setHighlighted:(BOOL)highlight {
    if (highlight) {
        [self.imageView setAlpha:.5];
    }  else {
        [self.imageView setAlpha:1];        
    }

//    [super setHighlighted:highlight];
}

In trying to figure this out, my method got a bit hackier as time went on, and I wound up subclassing UIButton and overriding setHighlighted:

For me it works to just knock down the image alpha to .5, because they're on a black background.

However, it only works if I comment out [super setHighlighted:] (where it appears the image-stretchy code is going on), which just doesn't feel like the right way to solve this at all...everything seems to be working fine, though. We'll see how it holds up as I keep working on it.

- (void)setHighlighted:(BOOL)highlight {
    if (highlight) {
        [self.imageView setAlpha:.5];
    }  else {
        [self.imageView setAlpha:1];        
    }

//    [super setHighlighted:highlight];
}
倾城花音 2024-09-11 11:23:54

如果有人正在寻找适用于 iOS 6 和 iOS 7 以及 Storyboard 的答案:

您可以在 Storyboard 中设置图像:

然后:

for(UIView* testId in self.subviews) {
    if([testId isKindOfClass:[UIImageView class]])
        [testId setContentMode:UIViewContentModeScaleAspectFill];
}

If anyone looking for answer that work in iOS 6 and iOS 7 and storyboard:

You can set image in your storyboard:

enter image description here

And then:

for(UIView* testId in self.subviews) {
    if([testId isKindOfClass:[UIImageView class]])
        [testId setContentMode:UIViewContentModeScaleAspectFill];
}
甜心 2024-09-11 11:23:54

如果 UIButton 似乎不听布局约束设置,请检查图像是否大于按钮大小。始终使用 @2x 和 @3x 图像来实现视网膜分辨率。

If the UIButton does not seem to listen to the layout constraint settings, do check whether the images are larger than the button size. Always use the @2x and @3x images for retina resolutions.

別甾虛僞 2024-09-11 11:23:53

我也有同样的问题。我发现这个问题有点老了,但我想提供一个清晰且正确的答案,以节省其他人(像我)在搜索结果中弹出时的时间。

我花了一些时间进行搜索和实验,但我找到了解决方案。只需设置 UIButton 内“隐藏”ImageView 的 ContentMode 即可。

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

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

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 alluding to in his accepted answer, but I suspect not.

凡尘雨 2024-09-11 11:23:53

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

或者执行此操作:

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

或相应地调整图像大小。

或者只使用 UIImageView(正确尊重 contentMode),并附加一个 UITapGestureRecognizer,或者在其顶部添加一个透明的 UIButton。

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

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.

画尸师 2024-09-11 11:23:53

您不需要在按钮本身上设置 contentMode,而是需要设置 contentHorizo​​ntalAlignmentcontentVerticalAlignment 属性以及(至关重要的)contentMode 用于按钮的 imageView 用于任何类型的宽高比填充或适合:

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

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

button.contentVerticalAlignment = .top

Rather than setting the contentMode on the button itself, you'll want to set contentHorizontalAlignment and contentVerticalAlignment properties and (crucially) the contentMode for the button's imageView for any kind of aspect fill or fit:

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

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

button.contentVerticalAlignment = .top
凉城已无爱 2024-09-11 11:23:53

经过几个小时的困惑,以下是我如何让它在 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];

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];
望喜 2024-09-11 11:23:53

答案是使用 UIImageView 以及您想要的所有可爱的内容模式设置,然后在其顶部放置一个自定义按钮。愚蠢的是你不能一次性完成所有这些,但看起来你不能。

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.

纵情客 2024-09-11 11:23:53

这两件事(最初很难找到)将拉伸您的 UIButton 图像以适合按钮大小:

在此处输入图像描述
输入图像描述这里

人们应该始终尝试在故事板而不是代码中进行设置。

These two things (which are quite hard to find initially) will stretch your UIButton image to fit the button size:

enter image description here
enter image description here

one should always try to set such in the Storyboard rather than code.

落花浅忆 2024-09-11 11:23:53

找到了解决这个问题的方法。将 UIButtonadjustsImageWhenHighlighted 属性设置为 NO

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

希望这有帮助。欢迎在下面评论,我会跟进您的任何问题。

Found a fix for this. Set the adjustsImageWhenHighlighted property of UIButton to NO.

  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.

德意的啸 2024-09-11 11:23:53

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

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

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;
}
自由如风 2024-09-11 11:23:53

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

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

Only solution which worked for me:

[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
邮友 2024-09-11 11:23:53

斯威夫特3

self.firstButton.imageView?.contentMode = .scaleAspectFill

Swift 3

self.firstButton.imageView?.contentMode = .scaleAspectFill
优雅的叶子 2024-09-11 11:23:53

对于在 iOS 15 和 Xcode 13 上遇到此问题的任何人,请参阅 Matt 在其他问题中的回答

Xcode 的行为发生了变化,现在将库中的 UIButtons 默认为 plain 样式,这会阻止子图像按预期缩放。

For anyone experiencing this on iOS 15 and Xcode 13, see Matt's answer in this other question.

The behavior of Xcode changed and now defaults UIButtons from the library to the plain style, which prevents the child image from scaling as expected.

月下凄凉 2024-09-11 11:23:53

尝试 setBackgroundImage 而不是 setImage

Instead of setImage try setBackgroundImage

早茶月光 2024-09-11 11:23:53

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文