一般可以巧用UIBUtton的UIControlStateSelected属性,来设置UIButton的选中背景图片。
- (UIButton *)normalButton
{
if (!_normalButton)
{
UIImage *_ljImage = [UIImage imageNamed:@"lj_unselect.png"];
_normalButton = [[UIButton alloc] initWithFrame:CGRectMake(kDEVICEWIDTH/2 - 80 - 40, 81 - 15, 50, 50)];
//_normalButton.backgroundColor = [UIColor redColor];
_normalButton.tag = 1000;
[_normalButton setImage:[UIImage imageNamed:@"lj_unselect.png"] forState:UIControlStateNormal];
[_normalButton setImage:[UIImage imageNamed:@"lj_select.png"] forState:UIControlStateSelected];
[_normalButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[_normalButton setSelected:YES]; //默认选中状态
}
return _normalButton;
}
- (UIButton *)bigButton
{
if (!_bigButton)
{
UIImage *_ljImage = [UIImage imageNamed:@"lj_unselect.png"];
_bigButton = [[UIButton alloc] initWithFrame:CGRectMake(kDEVICEWIDTH/2 + 42 -10, 81 - 15, 50, 50)];
//_bigButton.backgroundColor = [UIColor redColor];
_bigButton.tag = 2000;
[_bigButton setImage:[UIImage imageNamed:@"lj_unselect.png"] forState:UIControlStateNormal];
[_bigButton setImage:[UIImage imageNamed:@"lj_select.png"] forState:UIControlStateSelected];
[_bigButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _bigButton;
}
/*
字体选中按钮点击
*/
- (void)buttonClick:(UIButton *)sender
{
sender.selected = !sender.selected;//状态取反
if (sender.tag == 1000) //标准
{
[_bigButton setSelected:NO];
}
else if (sender.tag == 2000)//大
{
[_normalButton setSelected:NO];
}
}
效果图: