0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift5]コードオンリーのUITabBarController

0
Posted at

Main.storyboardは削除した状態が前提です。

まずはUITabBarControllerを継承したクラスを作ります。ここでは"TabController"

class TabController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var viewControllers = [UIViewController]()
        
        let firstViewController = FirstViewController()
        firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .mostRecent, tag: 1)
        viewControllers.append(firstViewController)
        
        let seccondViewController = SeccondViewController()
        seccondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .mostRecent, tag: 2)
        viewControllers.append(seccondViewController)
        
        let thirdViewController = ThirdViewController()
        thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .mostRecent, tag: 3)
        viewControllers.append(thirdViewController)
        
        self.setViewControllers(viewControllers, animated: false)
        self.selectedIndex = 1
    }
}

続いてSceneDelegateでTabControllerを初期画面に設定

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        
        guard let windowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(windowScene: windowScene)
        self.window = window
        
        window.rootViewController = TabController()
        window.makeKeyAndVisible()
    }

あとはFirstVC, SeccondVC, ThirdVCをお好きなようにカスタマイズしてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?