我創建了一個按鈕,我把它放在與標籤欄this guide。保持按鈕選擇或突出顯示在特定的標籤欄selectedindex
我希望此按鈕只有在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;
}
如果有必要,代碼在加載的TabBar按鈕:
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];
我把它叫做'內應用didFinishLaunchingWithOptions' 你能不能舉個例子,一直在努力使其工作,但是當我用' - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1 {NSLog(@「VC1 selected」);}'它爲我選擇的每個viewcontroller運行nslog代碼。 – Keaulhaas
是的,它會顯示相同的輸出,因爲您沒有指定條件。我用一個例子編輯了我的答案。 –
謝謝。接受了答案。 我不得不在**我的代碼**中改變一些東西以使其起作用。也許這會幫助其他人: 它沒有工作的第一個原因是我改名SecondViewController爲別的東西。出於某種原因,它無法拾取'viewController.title',即使是其他一切正常工作。使用xib創建一個新文件並添加解決該問題的文件。 其他原因,爲什麼它不工作:我用按鈕設置'self.tabBarController.selectedIndex = 2',我不得不刪除按鈕,並獲得突出顯示的效果,我添加一個圖像子視圖時,條件改變。 – Keaulhaas