Setting the default tab when using storyboards

前端 未结 12 813
执笔经年
执笔经年 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:43

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

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

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

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

    in appdelegate find applicationDidBecomeActive function and add this lines

    let tabBarController = self.window?.rootViewController as! UITabBarController
    tabBarController.selectedIndex = 0 // any index you want
    
    0 讨论(0)
  • 2020-12-12 16:52

    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!

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

    The following code worked for me:

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.selectedIndex = 2;
    
    0 讨论(0)
提交回复
热议问题