简体   繁体   中英

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!

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM