2018年3月31日土曜日

TabBarからViewに遷移

前回TabBarにNavigationという記事を書きました。
  • Xcode 8.3.3
  • Swift 3.1
今回はこれに加えてタブのアイテムにタブ表示に依存しないViewを追加したいと思います。
モーダルも以下コードで再現出来るので参考にして頂ければと。
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import UIKit
 
class TabViewController: UITabBarController, UITabBarControllerDelegate {
  
 class DummyViewController: UIViewController {}
 
 private var _Example1: UINavigationController!
 private var _Example2: UINavigationController!
 private var _Example3: UINavigationController!
 private var _Modal: DummyViewController!
  
 override func viewDidLoad() {
  super.viewDidLoad()
   
  self.view.backgroundColor = UIColor.white
   
  _Example1 = UINavigationController(rootViewController: Example1ViewController())
  _Example2 = UINavigationController(rootViewController: Example2ViewController())
  _Example3 = UINavigationController(rootViewController: Example3ViewController())
  _Modal = DummyViewController()
   
  _Example1.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 1)
  _Example2.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 2)
  _Example3.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 3)
  _Modal.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.featured, tag: 4)
   
  self.setViewControllers([_Example1!, _Example2!, _Example3!, _Modal!], animated: false)
  self.delegate = self
 }
  
 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
 }
 
 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  if viewController is DummyViewController {
   let example = ExampleViewController()
   example.modalPresentationStyle = .overCurrentContext
   self.present(example, animated: false, completion: nil)
   return false
  }
  return true
 }
}

これで_Modalに該当するアイテムをタップした時だけタブ内ではなく、ExampleViewControllerを重ねて表示します。

ExampleViewControllerの背景を黒透過にして白いレイヤを表示するよう制御すれば、モーダルを再現出来るかと🙆

0 件のコメント:

コメントを投稿