iOS UIButton图像contentMode在TouchUpInside上闪烁

时间:2012-03-22 09:20:32

标签: iphone ios uibutton touch contentmode

如果我将其imageView contentMode设置为UIViewContentModeScaleAspectFill,我的UIButton图片会出现问题。

UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageButton setImage:[UIImage imageNamed:@"myImage.jpg"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(doSmth:) forControlEvents:UIControlEventTouchUpInside];

按钮内部的图像被正确缩放以填充整个按钮区域。

点击/触摸按钮后,图像会调整大小(闪烁),就像它的contentMode设置为UIViewContentModeScaleAspectFit一样。

有人知道在点击/触摸时如何消除这种闪烁吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

为什么要设置按钮的imageView的内容模式?您为指定状态设置的图像始终填充按钮的大小。因此,您必须将按钮的框架大小设置为图像的大小,而不是设置 imageView contentMode

闪烁可能是按钮的高亮显示。如果要设置自定义图像以突出显示,请使用以下代码:

[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]

答案 1 :(得分:0)

只需为 highlighted adjusts image 禁用/取消选中 attributes inspector 中的 imageButton