###前提・実現したいこと
現在iOSアプリケーションを開発しています。
その開発の中でUITabBarControllerを使ってタブバーの部分を実装しています。そこで、タブバーのアイテムをタップして画面遷移をする時に「present modally」のような形で画面遷移を実装したいと考えています。
しかし、tabbarcontrollerを使った画面遷移の仕方しかわからず困っております。
インスタグラムのタブバーの真ん中のボタン(投稿ボタンのことです)をおした時のような実装をイメージしています。
調べた所、以下の書き方で実装出来るというような記事を何個か見たのですがうまく出来ません。
swift
1func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 2 if viewController is PostViewController { 3 if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "Post"){ 4 print("hoge") 5 tabBarController.present(newVC, animated: true, completion: nil) 6 return false 7 } 8 } 9 print("piyo") 10 return true 11 }
何かいい方法をご存知でしたらぜひご教示願いたいです。
###該当のソースコード
swift
1class TabBarController: UITabBarController, UITabBarControllerDelegate { 2 3 override func viewDidLoad() { 4 super.viewDidLoad() 5 6 self.tabBarController?.delegate = UIApplication.shared.delegate as? UITabBarControllerDelegate 7 8 // TabBarの背景色の設定 9 UITabBar.appearance().backgroundColor = UIColor.white 10 11 // TabBarに表示する画像の用意 12 let tweetIcon = makeOriginalIcon(name: "tweet_icon@3") 13 let highlightedTweetIcon = makeOriginalIcon(name: "tweet_icon_selected@3") 14 let myPageIcon = makeOriginalIcon(name: "mypage_icon@3") 15 let highlightedMyPageIcon = makeOriginalIcon(name: "mypage_icon_selected@3") 16 let timelineIcon = makeOriginalIcon(name: "timeline_icon@3") 17 let highlightedTimelineIcon = makeOriginalIcon(name: "timeline_icon_selected@3") 18 19 // 遷移後のViewControllerの取得 20 let timelineViewController = self.viewControllers![0] 21 let postViewController = self.viewControllers![1] 22 let myPageViewController = self.viewControllers![2] 23 24 // それぞれに画像を指定する 25 timelineViewController.tabBarItem = UITabBarItem(title: nil, image: timelineIcon, selectedImage: highlightedTimelineIcon) 26 postViewController.tabBarItem = UITabBarItem(title: nil, image: tweetIcon, selectedImage: highlightedTweetIcon) 27 myPageViewController.tabBarItem = UITabBarItem(title: nil, image: myPageIcon, selectedImage: highlightedMyPageIcon) 28 } 29 30 override func didReceiveMemoryWarning() { 31 super.didReceiveMemoryWarning() 32 // Dispose of any resources that can be recreated. 33 } 34 35 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 36 if viewController is PostViewController { 37 if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "Post"){ 38 tabBarController.present(newVC, animated: true, completion: nil) 39 return false 40 } 41 } 42 return true 43 } 44} 45
###試したこと
試したこととしては、上に書いたとおりのことで実装を試みました。ですが出来ていません。
###補足情報(言語/FW/ツール等のバージョンなど)
まだまだ初心者なので情報に不足などがあればご指摘をお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/25 05:35