UITabBarController Set a Default Tab in Swift Lang

2019-08-04 11:48发布

I have a UITabBarController that has 5 different view controllers attached to it. Each one has there own specific tab such as Home, Settings, etc. How can i set a certain tab to be the default tab when launched?

2条回答
何必那么认真
2楼-- · 2019-08-04 12:02

Is there a specific reason why you can't move your chosen first tab to be presented to the left most slot?

self.tabBarController.selectedIndex = desiredIndex

or, if you're code is in a UITabBarConroller subclass, simply:

 selectedIndex = desiredIndex

should work.

查看更多
相关推荐>>
3楼-- · 2019-08-04 12:14

You could consider changing the tab in the app delegate's didFinishLaunchingWithOptions (or whatever owns the tab bar controller) with code like this:

if let tabController = window?.rootViewController as? UITabBarController {
        tabController.selectedIndex = 1
    }
查看更多
登录 后发表回答