Setting the default tab when using storyboards

前端 未结 12 812
执笔经年
执笔经年 2020-12-12 16:27

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

相关标签:
12条回答
  • 2020-12-12 16:32

    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! (:
    0 讨论(0)
  • 2020-12-12 16:34

    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]
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 16:35
    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.

    0 讨论(0)
  • 2020-12-12 16:36

    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:

    0 讨论(0)
  • 2020-12-12 16:39

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

    0 讨论(0)
  • 2020-12-12 16:42

    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

    0 讨论(0)
提交回复
热议问题