简体   繁体   English

按下按钮时如何更改按钮图像

[英]how to change button image when button pressed

我想在用户点击该按钮后显示该按钮的其他图像...我该怎么做

You can define images for different states of button press. 您可以为按钮的不同状态定义图像。 Here is a brief example: 这是一个简单的示例:

UIButton *submitbutton = [UIButton buttonWithType:UIButtonTypeCustom];
// position in the parent view and set the size of the button
submitbutton.frame = CGRectMake(165, 20, 149, 39); 
[submitbutton setTitle:@"Submit Booking" forState:UIControlStateNormal];

// Add image to button for normal state
UIImage * btnImage1 = [UIImage imageNamed:@"SubmitBooking-normal.png"];
[submitbutton setImage:btnImage1 forState:UIControlStateNormal];

// Add image to button for pressed state
UIImage * btnImage2 = [UIImage imageNamed:@"SubmitBooking-pressed.png"];
[submitbutton setImage:btnImage2 forState:UIControlStateHighlighted];


// add targets and actions
[submitbutton addTarget:self action:@selector(submitBookingButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// add to a some parent view.
[someview addSubview:submitbutton];

There are probably other things you would want to customize. 您可能还需要自定义其他内容。 You can read more about these options here 您可以在此处阅读有关这些选项的更多信息

another way is to choose the button and assign the image you want then from the utilities bar change the state config to Highlighted then assign the on click image 另一种方法是选择按钮并分配所需的图像,然后从实用程序栏中将状态配置更改为“突出显示”,然后分配单击图像

see the link for screen shot below 请参阅下面的屏幕快照链接

screen shot 屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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