Setting default tab in UITabBar in swift

前端 未结 4 765
傲寒
傲寒 2020-12-28 22:57

I\'m almost finished with a project and am working out the last few UI kinks. My app uses a tab bar to navigate and for aesthetic purposes I want the app to open on the last

相关标签:
4条回答
  • 2020-12-28 23:27

    There is a nice way to do this straight from interface builder on your tab bar controller click the identity inspector, and set a User Defined Runtime Attribute.

    In this case each index of the tab bar control is indexed like a 0 based array. So the far left is 0, next to the right is 1, then 2 etc...

    So in my case I wanted 'tasks' to be selected first, so you set the selectedIndex to type Number and value '1'. Refer to my attached image.

    0 讨论(0)
  • 2020-12-28 23:28

    In the First items view controller write

    tabBarController?.selectedIndex = 2//required value
    
    0 讨论(0)
  • 2020-12-28 23:31

    Just set the selectedIndex of the tabBarController. Something along these lines.

    var freshLaunch = true
    override func viewWillAppear(animated: Bool) {
         if freshLaunch == true {
             freshLaunch = false
             self.tabBarController.selectedIndex = 4 // 5th tab
         }
    }
    
    0 讨论(0)
  • 2020-12-28 23:43

    If you're using UISegue from UIStoryBoard, you can use this in UIViewController where you're performing UISegue

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let tabBarController = segue.destination as? UITabBarController
        tabBarController?.selectedIndex = 1
    }
    

    or

    class TabBarController: UITabBarController {
    
       override func viewDidLoad() {
           super.viewDidLoad()
           self.selectedIndex = 1
       }
    
    }
    

    this code in your UITabBarController itself.

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