Programmatically switching between tabs within Swift

后端 未结 8 563
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 02:32

I need write some code to switch the view to another tab when the iOS app starts (so, for example, the second tab is shown by default rather than the first).

I\'m ne

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 03:30

    Just to update, following iOS 13, we now have SceneDelegates. So one might choose to put the desired tab selection in SceneDelegate.swift as follows:

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, 
                   willConnectTo session: UISceneSession, 
                   options connectionOptions: UIScene.ConnectionOptions) {
    
            guard let _ = (scene as? UIWindowScene) else { return }
    
            if let tabBarController = self.window!.rootViewController as? UITabBarController {
                tabBarController.selectedIndex = 1
            }
    
        }
    

提交回复
热议问题