我创建了一个按钮,然后将其放在带有this guide的de tabbar上。
我希望仅当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];
答案 0 :(得分:1)
您在哪里调用按钮突出显示代码?我认为每次触摸标签时都需要调用它(实现UITabBarControllerDelegate
方法tabBarController:didSelectViewController:
以了解用户切换标签的时间。)
示例:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if([viewController.title isEqualToString:@"View controller 1"]) {
NSLog(@"View controller 1 was selected");
} else {
NSLog(@"Some other view controller was selected");
}
}
答案 1 :(得分:0)
iv将此添加到我的自定义tabbar.m文件中。
- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
NSLog(@"Tab index = %u", indexOfTab);
if (indexOfTab == 0) {
[button setSelected:false];
}
if (indexOfTab == 1) {
[button setSelected:false];
}
if (indexOfTab == 3) {
[button setSelected:false];
}
if (indexOfTab == 4) {
[button setSelected:false];
}
}
- (void)buttonPressed:(id)sender {
[button setSelected:true];
[self setSelectedIndex:2];}
这对我有用,希望它有助于其他人......