我创建了一个按钮,并使用本指南将其放在标签栏上。
我希望仅当 selectedIndex 为 2 时才突出显示/选择此按钮。当 selectedIndex 为 0 1 3 4 时,我不希望其处于正常状态。这可能吗?
我尝试了以下操作,它突出显示了该按钮,但是一旦我单击该按钮,突出显示就会消失并且不会返回。
if (self.tabBarController.selectedIndex == 2) {
button.selected = YES;
button.highlighted = YES;
}else {
button.selected = NO;
button.highlighted = NO;
}
如有必要,在标签栏上加载按钮的代码:
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonFrame = button.frame;
buttonFrame.size.height = 55;
buttonFrame.size.width = 64;
buttonFrame.origin.x = 128;
buttonFrame.origin.y = 424;
button.frame = buttonFrame;
[button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttonhighlight.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"buttonhighlight.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_tabBarController.view addSubview:button];