UIButton Highlighted - UIControlStateHighlighted


 

UIButton Highlighted - UIControlStateHighlighted

In this example we are going to change the image of button while highlighting it. "Highlighted" is the state of control that occurs when touch enters and exits on control.

In this example we are going to change the image of button while highlighting it. "Highlighted" is the state of control that occurs when touch enters and exits on control.

UIButton Highlighted - UIControlStateHighlighted

In this example we are going to change the image of button while highlighting it. "Highlighted" is the state of control that occurs when touch enters and exits on control.

In this example, we are going to create a UIButton programmatically that will show the button changing on touch up.

Create a button

- (void)drawButton
{
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(80, 50, 70, 70); //set frame for button

UIImage *buttonImage = [UIImage imageNamed:@"icon6.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

[myButton setTitle:@"Ok" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:myButton];

}

- (void)viewDidLoad {
[super viewDidLoad];

[self drawButton];

}

Write a Button Action, that will change the image on touch up


- (IBAction)buttonClicked:(id)sender
{

UIImage *buttonImage = [UIImage imageNamed:@"home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];

}

On running the application you will find that it changes the image on touch up.

Download Code

Ads