简体   繁体   中英

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

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