简体   繁体   中英

Change background image of clicked UIButton created programmatically

I have created UIButtons programatically and added them to my UIScrollView .Now I want to change the background image of clicked UIButton to an image and others should remain in blue color which is the default color.Now when I click another UIButton , I want to change the previously clicked UIButton to blue and currently clicked button to have the bg image.

This is the code for creating and setting UIButton properties:

   for (int i=0; i<[arrPartVenuDetails count]; i++) {

        NSString *venueId = [[arrPartVenuDetails objectAtIndex:i] objectForKey:@"venue_id"];
        NSInteger venue_id= [venueId integerValue];
        NSLog(@"venue id %ld",(long)venue_id);

        aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [aButton setFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.f)];

        [aButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
        NSString *buttonClassName = [NSString stringWithFormat:@"VENUE %d", i+1];
        [aButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

        [aButton setTitle:buttonClassName forState:UIControlStateNormal];
        aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
        [aButton addTarget:self action:@selector(venueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        aButton.tag = venue_id;
        [scrollVenue addSubview:aButton];

        yCoord += buttonHeight + buffer;
    }

This is button click method:

-(void) venueButtonClick:(UIButton *)sender{

    [sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];

    CGRect btnOnclickFrame = sender.frame;
    btnOnclickFrame.size.width = 210;
    sender.frame = btnOnclickFrame;
 }

Modify your venueButtonClick as follows:

-(void) venueButtonClick:(UIButton *)sender{
     for (UIButton *btn in scrollVenue) {

        if (btn.tag==sender.tag) {

            [btn setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
        }
        else{

            [btn setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
        }
    }
}

Add a new property to your .h file.

@property NSUInteger lastSelectedTag;

In your viewDidLoad , assign it some safe value:

_lastSelectedTag = 80807652;//example value as it will be unique

Now in your IBAction , change it like this:

-(void) venueButtonClick:(UIButton *)sender{

    if(_lastSelectedTag!=80807652)
    {
        //lastSelectedTag is different than our safe value so it means that it now contains tag of real last selected button
        UIButton *lastSelectedButton = [scrollVenue viewWithTag:_lastSelectedTag]; //This is the previously selected button

        //Change it to blue and do whatever you want
       [lastSelectedButton setBackgroundImage:nil forState:UIControlStateNormal];//remove background image
       [lastSelectedButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]]; //give it color

    }
   [sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];

    CGRect btnOnclickFrame = sender.frame;
    [aButton setBackgroundColor:[UIColor clearColor]];
    btnOnclickFrame.size.width = 210;
    sender.frame = btnOnclickFrame;
    _lastSelectedTag = sender.tag; //Set lastSelectedButton to this button's tag
}

By the way, a suggestion, you will be better off using UITableView instead of UIScrollView .

There are two other possibilities:

  1. If your creating 3-4 button you might be better using UISegmentedControl which will allow you to customise it and save you having to write all that extra code.

在此处输入图片说明

See the apple docs for details on how to customise

  1. If your creating many buttons then a UITableView might be more suitable, and use the selected property of the UITableViewCell , you will need to create a custom UITableViewCell .

Hope this helps.

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