Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the default tab when using storyboards

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

like image 296
user1145581 Avatar asked Oct 30 '12 10:10

user1145581


People also ask

What is a tab bar controller?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


2 Answers

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:

like image 87
Joshua Finch Avatar answered Sep 28 '22 10:09

Joshua Finch


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! (:
like image 25
Aviel Gross Avatar answered Sep 28 '22 09:09

Aviel Gross