Select no tabs in a UITabBar

后端 未结 7 1544
心在旅途
心在旅途 2020-12-06 19:23

I\'m trying to select no tabs at all in my application. At first the first tab is selected, but I\'d like to deselect it so no tabs at all would be selected.

Don\'t

相关标签:
7条回答
  • 2020-12-06 19:44

    You can deselect all tab bar items if you are using UITabBar instance without UITabBarController one.

    In such case below code works well.

    [tabBar setSelectedItem:nil];
    

    If UITabBar is a part of UITabBarController then application will crash with exception:

    'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

    In other words if you would like to get this worked you need to manage tabbar's routines manually without controller.

    0 讨论(0)
  • 2020-12-06 19:44

    Better to change selected image whenever you want & make a view hide or show according to your requirement. Here my piece of code that could help to understand:

    -(void)viewWillAppear:(BOOL)animated{
        if ([[NSUserDefaults standardUserDefaults]integerForKey:@"flagAsk"]) {
            UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
            firstTab.selectedImage = [[UIImage imageNamed:@"Ask2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
                    vieToHide.hidden=YES;
        }
        else{
            UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
            firstTab.selectedImage = [[UIImage imageNamed:@"Ask"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
            vieToHide.hidden=NO;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 19:50

    From the documentation:

    This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllers array. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndex property accordingly. The default value of this property is nil.

    So, I would assume you need to [rootController setSelectedViewController: nil];.

    Update:

    To clarify a bit,

    [self.tabBarController setSelectedViewController:nil];

    There is also documentation on preventing the selection of tabs that could be helpful.

    0 讨论(0)
  • 2020-12-06 19:55

    I think rootController.tabBar.selectedItem = 0;

    it's wrong whatever you have tried. Because when You are setting selectedItem=0, then sure it will take the first tabBarItem of tabBarController.

    0 讨论(0)
  • 2020-12-06 19:56

    I just came across this question and it's actually really simple:

    tabBarController.selectedViewController = viewController;
    

    This is somewhat similar to HG's answer, but setting the selected view controller to nil is unnecessary.

    0 讨论(0)
  • 2020-12-06 20:02

    Are there better methods?

    use [self.tabBarController setSelectedViewController:nil],
    Warning : "-[UITabBarController setSelectedViewController:] only a view controller in the tab bar controller's list of view controllers can be selected."
    
    0 讨论(0)
提交回复
热议问题