我有两个分段按钮(1和2)我想为选定的分段添加一个背景图像,为未选择的分段添加另一个图像。我该怎么办?
这是我到目前为止,我应该在这里设置背景图片吗?:
- (void)selectWithSegment:(UIButton *)sender
{
for (UIButton *button in self.segments) {
if (button == sender) {
if (self.staySelected) {
button.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:17.0/255.0 blue:17.0/255.0 alpha:1.0];
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
if ([self.delegate respondsToSelector:@selector(segmentControl:didSelectSegment:)]) {
[self.delegate segmentControl:self didSelectSegment:sender];
}
} else {
[self decorateButton:button];
}
}
}
如何为选定和未选定的片段设置image1.png和image2.png?有人可以帮我实现吗?
答案 0 :(得分:0)
我不知道处理这种情况的任何其他方法,但我认为这可以帮助你:
在制作UISegmentControl的IBOutlet之后,使用以下操作将IBAction设置为valueChange事件:
-(IBAction)selectedSegment:(id)sender{
int totalNumberOfSegment = 5;
UISegmentedControl *tmpSig = (UISegmentedControl *)sender;
NSLog(@"Inside sender method");
for (int i = 0; i < totalNumberOfSegment; i++) {
[tmpSig setImage:[UIImage imageNamed:@"unselect.png"] forSegmentAtIndex:i];
}
[tmpSig setImage:[UIImage imageNamed:@"select.png"] forSegmentAtIndex:tmpSig.selectedSegmentIndex];
}