2018年3月27日火曜日

StoryBoardを使わずにUITabBarController

以前ブログにも書きましたがStoryBoardを使わずに開発をしており、今回はUITabBarControllerを実装していきます。
  • Xcode 8.3.3
  • Swift 3.1
タブに3つのアイテムを持ったTabBarControllerを作ります。
まぁそこまで難しいコードでもないです。Google先生に聞けばすぐ出てきます😅
TabViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 件のコメント:

コメントを投稿