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

时间:2010-08-05 18:45:22

标签: iphone xcode uibutton

我希望在用户点按该按钮后显示按钮的不同图像... 我该怎么做

2 个答案:

答案 0 :(得分:15)

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

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

您可能想要自定义其他内容。您可以阅读有关这些选项的更多信息here

答案 1 :(得分:0)

另一种方法是选择按钮并分配您想要的图像然后从实用工具栏将状态配置更改为突出显示然后分配点击图像

请参阅下面的屏幕截图链接

screen shot