Setting default tab in UITabBar in swift

安稳与你 提交于 2019-11-30 13:25:29

问题


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 tab (User Profile) instead of the first. I know I can reorder the tabs in Interface Builder but the tab bar would just look odd having the profile tab first and the home tab anywhere else. How can I leave the profile tab as the 5th tab yet by default open the app on that tab?


回答1:


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
     }
}



回答2:


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.




回答3:


In the First items view controller write

tabBarController?.selectedIndex = 2//required value


来源:https://stackoverflow.com/questions/30039717/setting-default-tab-in-uitabbar-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!