UIButton不听内容模式设置?

时间:2010-06-01 14:29:55

标签: ios uibutton contentmode

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

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

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

16 个答案:

答案 0 :(得分:178)

我遇到了同样的问题。我看到这个问题有点老了,但是我想提供一个清晰正确的答案来保存其他人(比如我)在搜索结果中弹出的时候。

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

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

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

答案 1 :(得分:65)

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

或者这样做:

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

或相应调整图片大小。

或者只使用附加了UITapGestureRecognizer的UIImageView(正确尊重contentMode),或者使用透明的UIButton。

答案 2 :(得分:45)

您需要设置contentModecontentHorizontalAlignment属性,并且(至关重要地)设置contentVerticalAlignment按钮contentMode,而不是在按钮上设置imageView。 1}}用于任何类型的方面填充或拟合。这是一个例子:

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 +)。

答案 3 :(得分:7)

经过几个小时的混乱,这就是我在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];

答案 4 :(得分:6)

答案是使用具有所需的所有可爱内容模式设置的UIImageView,然后在其上层叠自定义按钮。愚蠢,你不能一次性完成所有这一切,但似乎你不能。

答案 5 :(得分:4)

找到了解决此问题的方法。将adjustsImageWhenHighlighted的{​​{1}}属性设置为UIButton

NO

希望这会有所帮助。请随时在下面发表评论,我会跟进您的任何问题。

答案 6 :(得分:3)

只有对我有用的解决方案:

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

答案 7 :(得分:2)

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

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

答案 8 :(得分:2)

Swift 3

self.firstButton.imageView?.contentMode = .scaleAspectFill

答案 9 :(得分:1)

而不是setImage尝试setBackgroundImage

答案 10 :(得分:1)

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

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

答案 11 :(得分:0)

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

答案 12 :(得分:0)

在试图解决这个问题时,随着时间的推移,我的方法变得有些迟钝了,我结束了UIButton的子类化并覆盖了setHighlighted:

对我而言,它可以将图像alpha降低到.5,因为它们位于黑色背景上。

然而,它只有在我注释掉[super setHighlighted:](看起来图像弹性代码正在进行)时才有效,这根本不是解决这个问题的正确方法......一切但是,似乎工作得很好。我会继续努力,看看它是如何成立的。

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

//    [super setHighlighted:highlight];
}

答案 13 :(得分:0)

如果有人在寻找可在iOS 6和iOS 7以及故事板中使用的答案:

您可以在故事板中设置图片:

enter image description here

然后:

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

答案 14 :(得分:0)

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

答案 15 :(得分:0)

这两件事(一开始很难找到)会拉伸UIButton图像以适合按钮大小:

enter image description here enter image description here

应该始终尝试在情节提要中设置此类代码,而不是代码。

相关问题