Deselect or unselect all tabs in tabbar in iOS 5
Asked Answered
A

5

5

I am new to iOS development and I have started with IOS 5 directly. I have created a storyboard which consists of a tabview controller as its rootviewcontroller. I have put 2 tabs to it.

I want to deselect/unselect all the tabs initially. How do I do this? I have tried the following

 UIView *view = [[UIView alloc]initWithNibName:@"view" bundle:[NSBundle mainBundle]];
    [self.tabBarController setSelectedViewController:nil];
    [self.tabBarController setSelectedViewController:view];

where I have added a view with identifier "view".

But this didn't work, it gives error:

 unrecognized selector sent to instance

I also tried the following

[self.tabBarController.tabBar setSelectedItem:nil];

but it says

'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

I have tried this code in controller for the first tab. I want to do this because I want to put a default view on top of first tab view and hide it once the use is clicked on any of the tabs below.

Aureomycin answered 27/3, 2012 at 10:19 Comment(0)
M
9

I use this to clear any selected tabBarItems

[myTabBar setSelectedItem:nil];
Morice answered 13/6, 2012 at 4:49 Comment(1)
Any way to set it animated ?Homology
P
5

Old question. Just for the record, you can't deselect all the Tabs if the Tab Bar is managed by a TabBarController. The method:

[self.tabBar setSelectedItem:nil];

Works only if the Tab Bar is not managed from a Tab Bar Controller. If it is, there's no way to deselect all the tabs, there must be one selected at all time.

Pisgah answered 11/7, 2012 at 4:54 Comment(0)
P
1

This is a little hoakey. But I simply set the tint color in -viewWillAppear to gray when I wanted it to look like there was no selection and then set the tint color back to the "selected" color when I want to show the selection.

// since we cant turn off tabbar selection, change the color to make
// it look like nothing was selected.
self.tabBarController.tabBar.tintColor = [UIColor grayColor];  
-----------
// make sure tintcolor is turned on (just in case another module turned it off
self.tabBarController.tabBar.tintColor = self.view.tintColor;
Planoconvex answered 16/10, 2016 at 23:37 Comment(0)
T
0

I've found a solution that is probably not the best way to go about it but it worked for me ([self.tabBar setSelectedItem:nil]; didn't). I put my tab view in a parent view (myTabBarViewContainer) and then when I want to deselect my items I call removeFromSuperview on the parent view and re-init it :

[self.myTabBarViewContainer.view removeFromSuperview];

Then re-init it and Voila!

Not very pretty but it works...

Tunnel answered 17/6, 2013 at 12:14 Comment(0)
P
0

You Should need to use UITabBar. Not UITabBarController

deselect or unselect tabbar item swift 3.0

tabbar_Main.selectedItem = nil
Prue answered 7/2, 2017 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.