简体   繁体   中英

Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?

[myButton setImage:[UIImage imageNamed:@"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;

I have tried this code and it is not working.

You should use the setBackgroundImage:forState . The setImage:forState changes the image which is on the same depth level as the button title. Additionally the backgroundImage is automatically fit to the button's view bounds. The image is not.

实现该按钮的onTouchDown动作,并通过[btn setBackgroundImage: forState:]在该函数中设置按钮背景图像

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