2018年3月31日土曜日

TabBarからViewに遷移

前回TabBarにNavigationという記事を書きました。
  • Xcode 8.3.3
  • Swift 3.1
今回はこれに加えてタブのアイテムにタブ表示に依存しないViewを追加したいと思います。
モーダルも以下コードで再現出来るので参考にして頂ければと。
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 件のコメント:

コメントを投稿