繁体   English   中英

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

[英]how to change button image when button pressed

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

您可以为按钮的不同状态定义图像。 这是一个简单的示例:

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

您可能还需要自定义其他内容。 您可以在此处阅读有关这些选项的更多信息

另一种方法是选择按钮并分配所需的图像,然后从实用程序栏中将状态配置更改为“突出显示”,然后分配单击图像

请参阅下面的屏幕快照链接

屏幕截图

暂无
暂无

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

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