iphoneiosuibuttontouchcontentmode

iOS UIButton image contentMode flickering on TouchUpInside


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!


Solution

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