0

My application is a classic TabBar Application. I would like to disable the currently selected TabBarItem to prevent the user from taping on the same item again (this causes a small graphic glitch).

To do that, I disable the currently selected TabBar Item.

 [[[[self.tabBarController tabBar]items]objectAtIndex:currentIndex]setEnabled:FALSE];

My problem is that when I disable the item. It becomes partially dimmed. So the Item is selected and its enabled property is set to FALSE; Under these conditions, the dimmers (due to the setEnable:FALSE) "wins" and the TabBarItem image dimmed down instead of highlighted (because it is the one currently selected).

Is there a way to prevent the dimmed effect when setting the enable property to FALSE ? If not, is there another way to discard the selection of the "currently selected" item of a TabBar than setting its Enable property to FALSE ?

Fifo
  • 25
  • 2

2 Answers2

0

If not, is there another way to discard the selection of the "currently selected" item of a TabBar than setting its Enable property to FALSE ?

I think that's the better way to go, since all you want to do is prevent selection of the currently selected item. The delegate methods (UITabBarControllerDelegate) give you what need:

func tabBarController(tabBarController: UITabBarController, 
  shouldSelectViewController viewController: UIViewController) -> Bool {
    let vc = tabBarController.selectedViewController
    if viewController == vc {
        return false
    }
    return true
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Depending on the state of the selected TabBar view Controller ( which is a navigation controller) I automatically "push" another controller from the VieWillAppear method. One can see this spurious transition, even if the Push is made with NO Animation. Is doing this "dirty" ? – Fifo Nov 18 '14 at 05:10
  • The thing is that if a tab item is already selected, tapping it doesn't call `viewWillAppear:` for that tab item's view controller. So you have not fully described the odd thing you must doing that causes the "glitch". – matt Nov 18 '14 at 13:01
  • Are you sure ? I did put a break point in the ViewWillAppear method of the viewcontroller, navigate through the NavigationController, then Tap on the TabBaritem again. Sure enough it stops at my break point. – Fifo Nov 19 '14 at 04:24
  • Sorry. What I meant is this. Make a Tabbed Application. In the FirstViewController, implement `viewWillAppear`. Tapping repeatedly on the First tab bar item doesn't call `viewWillAppear` multiple times. That's because this tab is already selected. So you normally shouldn't have to do _anything_ in order to prevent this tab being selected when it is already selected. But you're right - you've navigated away from the navigation controller's root view controller, and selecting the tab item navigates back. – matt Nov 19 '14 at 05:08
  • However, as I already said, the delegate method will prevent the selection, thus solving the problem. I am tempted to regard the phenomenon you've discovered as a bug, but it's so easy to work around that it seems hardly worth worrying about whether it is or not... – matt Nov 19 '14 at 05:14
  • Super Matt! The shouldSelect delegate that you provided did the trick for me. I did not realize that returning FALSE will prevent the selection from happening... (we all have to learn something every day). Now on i will do that every time I have a navigation controller in a Tab Bar, because seeing the navigation going automatically through the controllers (like a movie) is not so good. Thx again ! – Fifo Nov 19 '14 at 18:26
  • You're very welcome. I'm still puzzled by the effect you've discovered. I was easily able to prove to myself, using just a storyboard and no code, that nav controller inside a tab view controller pops back to its root view controller when the user taps its tab bar item, even when this tab bar item was already the selected item. That's what's giving you trouble here. It seems like wrong behavior to me, but it could be totally intentional on Apple's part, so who knows? – matt Nov 19 '14 at 18:43
-1

try this... wright this code in you app delegate.this may help...

 [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
 [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
hmdeep
  • 2,910
  • 3
  • 14
  • 22