2018年3月27日火曜日

StoryBoardを使わずにUITabBarController

以前ブログにも書きましたがStoryBoardを使わずに開発をしており、今回はUITabBarControllerを実装していきます。
  • Xcode 8.3.3
  • Swift 3.1
タブに3つのアイテムを持ったTabBarControllerを作ります。
まぁそこまで難しいコードでもないです。Google先生に聞けばすぐ出てきます😅
import UIKit

class TabViewController: UITabBarController {
 
 private var _Example1: UIViewController!
 private var _Example2: UIViewController!
 private var _Example3: UIViewController!
 
 override func viewDidLoad() {
  super.viewDidLoad()
  
  self.view.backgroundColor = UIColor.white
  
  _Example1 = UIViewController()
  _Example2 = UIViewController()
  _Example3 = UIViewController()
  
  _Example1.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 1)
  _Example2.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 2)
  _Example3.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 3)
  
  self.setViewControllers([_Example1!, _Example2!, _Example3!], animated: false)
 }
 
 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
 }
}

タブのアイテム画像は全部featuredですが、実行すればTabBarControllerが確認出来ると思います!

0 件のコメント:

コメントを投稿