更改以编程方式创建的单击UIButton的背景图像

时间:2016-02-11 06:01:17

标签: ios objective-c iphone uibutton

我已经以编程方式创建了UIButtons并将其添加到我的UIScrollView。现在我想将点击的UIButton的背景图片更改为图片,其他人应该保持蓝色,这是默认颜色。现在当我点击另一个UIButton时,我想将先前点击的UIButton更改为蓝色且当前点击的按钮以获得bg图像。

这是用于创建和设置UIButton属性的代码:

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

这是按钮点击方法:

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

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

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

3 个答案:

答案 0 :(得分:0)

修改您的venueButtonClick,如下所示:

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

答案 1 :(得分:0)

.h文件中添加新属性。

@property NSUInteger lastSelectedTag;

viewDidLoad中,为其指定一些安全值:

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

现在在IBAction中,将其更改为:

-(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
}

顺便提一句,建议您最好使用UITableView代替UIScrollView

答案 2 :(得分:0)

还有两种可能性:

  1. 如果您创建3-4按钮,您可能会更好地使用UISegmentedControl,这样您就可以自定义它并节省您必须编写所有额外代码的费用。
  2. enter image description here

    See the apple docs for details on how to customise

    1. 如果您创建了许多按钮,那么UITableView可能更合适,并使用UITableViewCell的所选属性,则需要创建自定义UITableViewCell
    2. 希望这有帮助。