
TabBarControllerへの画面遷移では、特に指定しなければ一番左側の画面に遷移します。
これを、任意の画面に遷移できるようにしましょう。
いくつかやり方があるみたいですが、私はこれでやりました(できました)的な記事なので、完全なものではありません。
【.selectedIndex = 数字】でOK
私が実装したのは下の画像のような「次へボタン」を押して上の画像にある「タイムライン」ではなく「遊ぶ」に遷移するというものです。
fa-chevron-circle-right実際のコード
1 2 3 4 5 6 7 |
@IBAction func next(_ sender: Any) { let tabbarController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarControllerID") as! UITabBarController tabbarController.selectedIndex = 1 self.navigationController?.pushViewController(tabbarController, animated: true) } |
storyboardIDで遷移しています。
1行目の let tabbarController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarControllerID") as! UITabBarController
これは"TabBarControllerID"というIdentifierのTabBarControllerに遷移するという意味になります。
2行目の tabbarController.selectedIndex = 1
これは定数tabbarControllerのインデックスに1を代入しています。
1というのはつまり、「遊ぶ」画面のことです。tabbarController[1]と同じ意味です。(1番上の画像参照)
tabbarController[0] = "tabbarController.selectedIndex = 0" = 「タイムライン」
tabbarController[1] = "tabbarController.selectedIndex = 1" = 「遊ぶ」
tabbarController[2] = "tabbarController.selectedIndex = 2" = 「マイページ」
3行目のself.navigationController?.pushViewController(tabbarController, animated: true)
これについては私がナビゲーションコントローラーで遷移しているためです。
TabBarを非表示にするには
1 2 3 |
//tabBarの非表示 tabBarController?.tabBar.isHidden = true |
表示したくなったらtrueをfalseにしれば良いです。