UIButton不听内容模式设置?

firstButton是Custom类型的UIButton。我以编程方式将三个放在一个表的每个单元格中,因此:
[firstButton setImage:markImage forState:UIControlStateNormal];
[firstButton setContentMode:UIViewContentModeScaleAspectFit];
[cell.contentView addSubview:firstButton];
在其他地方,我告诉它clipToBounds。我得到的是图像中心方块的裁剪,而不是它的纵横比例渲染。我已经尝试了很多方法,包括在firstButton.imageView上设置mode属性,这似乎也不起作用。     
已邀请:
我有同样的问题。我看到这个问题有点老了,但是我想提供一个明确而正确的答案来保存其他人(比如我)在搜索结果中弹出的时候。 它花了我一些搜索和实验,但我找到了解决方案。只需设置UIButton中“隐藏”ImageView的ContentMode即可。
[[firstButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[firstButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
也许这就是丹雷在他接受的答案中提到的,但我怀疑不是。     
如果您正在处理UIButton的图像(而不是它的backgroundImage,则在UIButton本身或其imageView上设置contentMode没有任何效果(尽管有其他答案说)。 或者这样做:
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
或相应调整图像大小。 或者只使用附加了UITapGestureRecognizer的UIImageView(正确地尊重contentMode),或者使用透明的UIButton。     
对于任何类型的方面填充或拟合,您不需要在按钮本身上设置
contentMode
,而是设置
contentHorizontalAlignment
contentVerticalAlignment
属性以及(关键地)按钮
imageView
contentMode
。这是一个例子:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
当然,您还可以执行其他操作,例如将按钮的图像对齐到按钮的顶部。如果您不需要填充或适合宽高比,则可以单独设置对齐:
button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
在Swift中,你会想要使用:
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .scaleAspectFit
这适用于所有iOS版本,包括具有自动布局的最新版本(即iOS 4至iOS 11+)。     
经过几个小时的混乱,这就是我在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];
    
答案是使用具有所需的所有可爱内容模式设置的UIImageView,然后在其上层叠自定义按钮。愚蠢,你不能一次性完成所有这一切,但似乎你不能。     
找到了解决方案。将
UIButton
adjustsImageWhenHighlighted
属性设置为
NO
  UIButton *b = [[UIButton alloc] initWithFrame:rect];
        [b setImage:image forState:UIControlStateNormal];
        [b.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [b setAdjustsImageWhenHighlighted:NO];
希望这可以帮助。请随时在下面发表评论,我会跟进您的任何问题。     
只有对我有用的解决方案:
[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    
我的回答与库巴相似。我需要以编程方式设置我的图像。
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;
}
    
斯威夫特3
self.firstButton.imageView?.contentMode = .scaleAspectFill
    
而不是setImage尝试setBackgroundImage     
我相信我们在这里有一个简单的界面构建器问题 - 显然,IB在您设置了图像属性后忽略了任何内容模式更改。 解决方案很简单:设置内容模式,删除以前设置的图像名称(确保在所有状态下删除它,默认,突出显示等),然后在所有需要的状态下重新输入所需的图像名称 - etvoilà 。     
我也建议看一下
adjustsImageWhenHighlighted
UIButton
属性,以避免按下按钮时图像出现奇怪的变形。     
在试图解决这个问题时,随着时间的推移,我的方法变得有些迟钝了,我结束了UIButton的子类化并重写了setHighlighted: 对我而言,它可以将图像alpha降低到.5,因为它们位于黑色背景上。 但是,只有当我注释掉[super setHighlighted:](看起来图像弹性代码才会出现)时它才有效,这根本就不是解决这个问题的正确方法......一切似乎都是但工作正常。我们会继续努力,看看它是如何起作用的。
- (void)setHighlighted:(BOOL)highlight {
    if (highlight) {
        [self.imageView setAlpha:.5];
    }  else {
        [self.imageView setAlpha:1];        
    }

//    [super setHighlighted:highlight];
}
    
如果有人在寻找可在iOS 6和iOS 7以及故事板中工作的答案: 您可以在故事板中设置图像: 然后:
for(UIView* testId in self.subviews) {
    if([testId isKindOfClass:[UIImageView class]])
        [testId setContentMode:UIViewContentModeScaleAspectFill];
}
    
如果UIButton似乎没有听取布局约束设置,请检查图像是否大于按钮大小。始终使用@ 2x和@ 3x图像进行视网膜分辨率。     

要回复问题请先登录注册