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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3418054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:29:49  来源:igfitidea点击:

how to change button image when button pressed

iphonexcodeuibutton

提问by Linux world

I want to show different image for button after user taps that button ... How would I do that

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

回答by JonB

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

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

回答by Diaa Den

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

截屏