0

I have a problem with UIButton image if I set its imageView contentMode to UIViewContentModeScaleAspectFill.

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

The image inside button is properly scaled to fill the whole button area.

After a click/touch on button the image gets resized (flickers) like if it's contentMode is set to UIViewContentModeScaleAspectFit.

Does anyone know how to remove this flickering when click/touch occurs?

Thanks!

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

2 Answers2

1

Why are you setting content mode of the button's imageView? The image, you set for specified state, is always filling the size of the button. So you have to set button's frame size to the size of the image and not to set imageView contentMode

The flickering could be the higlighting of the button. If you want to set custom image for highlighting, use this code:

[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]
Martin Pilch
  • 3,245
  • 3
  • 38
  • 61
  • so set the same image for the highlighted state :) if you do it, when you press the button, you will see no interaction – Martin Pilch Mar 22 '12 at 09:36
  • Or if I say with other words: I want the contentMode to be the same like when button is in normal state. – Borut Tomazin Mar 22 '12 at 09:38
  • Yes, I just set image for highlighted state (without image for normal state) and image only shows up when touch occurs. Afterwards I set image for both states and there is no flickering. But I don't think this is a good idea. Also button has lost it's touch animation... – Borut Tomazin Mar 22 '12 at 09:40
  • but you do not need to set contentMode. you have image of size 100x50 pixels, so create the button with frame size 100x50 pixels and do not set the contentMode – Martin Pilch Mar 22 '12 at 09:41
  • You can create another image for highlighted state which includes "touch animation" – Martin Pilch Mar 22 '12 at 09:42
  • The reason I used contentMode is cos images are not the same size as the button is. Some are 200x80, others are 800x60 etc. – Borut Tomazin Mar 22 '12 at 09:42
  • I don't get what you are trying to do. You can resize images to the same size using Preview, PhotoShop, Pixelmator... – Martin Pilch Mar 22 '12 at 09:44
  • I get this images dynamically so I don't see the reason to have another one just for touch animation. – Borut Tomazin Mar 22 '12 at 13:34
  • So you can programatically resize the image. Do you need the image to fill the button or to keep aspect ratio? If you need to fill the image, it is done automatically – Martin Pilch Mar 22 '12 at 13:42
  • I need both and that is well done by this great function: UIViewContentModeScaleAspectFill. However this does not work on touchUpInside action on button. – Borut Tomazin Mar 22 '12 at 13:45
  • so you should subclass UIView or UIImageView and implemented your own "button". I thinkh there is no way to do it using UIButton – Martin Pilch Mar 22 '12 at 13:58
0

Just disable/uncheck the highlighted adjusts image in attributes inspector for imageButton

nitish005
  • 106
  • 11