Setting the default tab when using storyboards

IosUitabbarcontrollerStoryboardUitabbar

Ios Problem Overview


Can anyone please tell me how to set the default tab when using storyboards in iOS. I can't seem to figure out how to accomplish this.

Thank you

Ios Solutions


Solution 1 - Ios

Whilst you can set the initial selected tab programmatically like the other answers, to achieve the same in your storyboard without touching code you would perform the following:

  1. Select the Tab Bar Controller in the Storyboard Interface
  2. Show the Identity Inspector in the Utilities panel
  3. Add a new "User Defined Runtime Attribute"
  4. Set the Key Path to "selectedIndex"
  5. Set the Type to "Number"
  6. Set the Value to the index of the tab you wish to select (a value of 1 would select the second tab for example)
  7. Save the Storyboard, build and run the application

This should be what it looks like when you've achieved the above steps:

Solution 2 - Ios

Might seem like overkill for some to subclass UITabBarController, but, I think it provides the cleanest solution.

  1. Create BaseTabBarController.swift

  2. Add an @IBInspectable and set it in viewDidLoad:

     class BaseTabBarController: UITabBarController {
    
         @IBInspectable var defaultIndex: Int = 0
    
         override func viewDidLoad() {
             super.viewDidLoad()
             selectedIndex = defaultIndex
         }
    
     }
    
  3. In the storyboard, set you UITabBarController to be your new subclass:

enter image description here

  1. Go to the Attributes Inspector add set the new property Default Index:

enter image description here

  1. ta-da! (:

Solution 3 - Ios

  1. Create a new file subclass of UITabBarController;

  2. Add this at the end of viewDidLoad:

    self.selectedIndex = 1;

  3. Set this new file as the Custom Class in the UITabBarController of your Storyboard.

You're done.

Solution 4 - Ios

The following code worked for me:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 2;

Solution 5 - Ios

You can use one of these two methods:

tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];

or a direct method from the object

[tabBar setSelectedItem:myUITabBarItem];

or you can combine them to do this:

tabBar.items = tabBarItems;
[tabBar setSelectedItem:[tabBarItems objectAtIndex:0]];

but i havent tested that method yet, hope this helps!

Solution 6 - Ios

in appdelegate find applicationDidBecomeActive function and add this lines

let tabBarController = self.window?.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0 // any index you want

Solution 7 - Ios

My variant is suitable when you want just change the default selected controller, no more customizing. Just add the follow category:

//  UITabBarController+DefaultPage.h
#import <UIKit/UIKit.h>

@interface UITabBarController(DefaultPage)

@end


//  UITabBarController+DefaultPage.m
#import "UITabBarController+DefaultPage.h"

@implementation UITabBarController(DefaultPage)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedIndex = 1;
}

@end

p.s: I prefer @joshua-finch answer

Solution 8 - Ios

In the viewDidLoad() of the TabBarController, set selectedIndex to whatever you want. (0 would be the first, 3 would be the fourth, etc.)

Solution 9 - Ios

You can achieve this through Xcode 8 or later (just tested it and don't know if it's available before this version)

Do the steps as @Joshua Finch said but:

  1. Select the bar item instead of the TabBar
  2. Got to "User Defined Runtime Attribute"
  3. Add new key
  4. Name it "selected"
  5. Set its type to boolean and choose true / or check the checkbox

Solution 10 - Ios

Building upon Aviel Gross' answer, I just wanted to implement this for a UITabBar, as opposed to a UITabBarController. This can be done as follows:

class BaseTabBar: UITabBar {

    @IBInspectable var defaultIndex: Int = 0 {
        didSet {
            self.selectedItem = self.items?[defaultIndex]
        }
    }
}

Solution 11 - Ios

Provided you're navigating to your UITabBarController from e.g. a login screen it might be least pain to set the selection there:

        let tabs = self.storyboard?.instantiateViewController(withIdentifier: "tabs") as! UITabBarController
        tabs.selectedIndex = 1
        self.present(tabs, animated:false)

Solution 12 - Ios

In InterfaceBuilder, disconnect all the segues, then reconnect them in the order you want them to appear.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1145581View Question on Stackoverflow
Solution 1 - IosJoshua FinchView Answer on Stackoverflow
Solution 2 - IosAviel GrossView Answer on Stackoverflow
Solution 3 - IosDevonDahonView Answer on Stackoverflow
Solution 4 - IosRahul SinghView Answer on Stackoverflow
Solution 5 - IosTrevor RudolphView Answer on Stackoverflow
Solution 6 - IoszizutgView Answer on Stackoverflow
Solution 7 - IosWINSergeyView Answer on Stackoverflow
Solution 8 - IosjakeView Answer on Stackoverflow
Solution 9 - Iosmohammad alnajjarView Answer on Stackoverflow
Solution 10 - IosThomasHazView Answer on Stackoverflow
Solution 11 - IosAnton DuzenkoView Answer on Stackoverflow
Solution 12 - IosMH175View Answer on Stackoverflow