[英]Change background image of clicked UIButton created programmatically
我已经以编程方式创建了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;
}
修改您的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]];
}
}
}
将新属性添加到您的.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
。
还有其他两种可能性:
UISegmentedControl
,它可以自定义按钮并省去编写所有额外代码的麻烦。 UITableView
可能更适合,并使用UITableViewCell
的selected属性,则需要创建一个自定义UITableViewCell
。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.