2017-09-06 7 views
2

タブバーコントローラーからビューをモーダルに表示すると、ビューが実際の表示に戻るようにできますか?タブバーコントローラーからモーダルビューを表示

カメラでビューを作成したいと考えています。 "WhatsApp"や "Instagram"のようなもので、途中にユーザーがクリックしてカメラビューが表示されるボタンがある場所。

さらに、ユーザーは、閉じるボタンをクリックしたときの前のタブを移動する必要があります。

This is how my ViewController is connected to the TabBarController

答えて

3

私は現在、建物だアプリでも似たような実装しなければならなかったが、それは比較的です直接行うには、これを実現するために、UITabBarControllerのデリゲートメソッドを実装する必要があります。

あなたが実装する必要がデリゲートメソッドは次のとおりです。このメソッドからfalseを返す tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

があなたのタブを選択すると、タブコントローラを停止します、あなたは、単にプログラム的UIViewControllerを提示するために、独自のロジックを実装する必要があります。私の実装が多少異なり、より多くの変数を持っているとして、私は上記のコードをテストしていませんでした

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 

    // If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller) 
    if viewController is YourViewControllerClass { 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass { 
      controller.modalPresentationStyle = .fullScreen 
      self.present(controller, animated: true, completion: nil) 
     } 

     return false 
    } 

    // Tells the tab bar to select other view controller as normal 
    return true 
} 

は、ここでの例です。一般的な原則は同じです。

どのように乗り遅れているか教えてください。必要に応じて回答を更新します。

+0

私はあなたのコードを使用しましたが、それはそれが動作するようにどのように動作していません。そこで、私のTabBarControllerのための新しいクラスを作成し、あなたのコードをそこに貼り付けました。それはあなたの意味ですか?そして、どのクラスをYourViewControllerClassの代わりに書くべきですか?多分私の元の投稿にリンクしている私の写真を見ることができます。 –

+0

ねえ、あなたは 'UITabBarController'をサブクラス化し、このクラスに' UITabBarDelegate'を実装する必要があります。 'viewDidLoad'では、' self.delegate = self'を実行する必要があります。そして、下のタブを選択しようとすると、上記のメソッドが起動します。 'YourViewControllerClass'は、あなたのストーリーボードのビューコントローラ(左のもの)に関連付けられたクラスです。 'UIViewController'をサブクラス化し、このクラスをそのView Controllerに適用する必要があります。 – WsCandy

+0

Yeey!できます。あなたの助けに感謝します。私はself.delegate = selfを忘れていました。今私がボタンでそれを閉じたい場合は、どのようにViewControllerに戻ることができますか? –

0
let modalVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifier") 
    modalVC.modalTransitionStyle = .crossDissolve 
    modalVC.modalPresentationStyle = .full or .overfullscreen // please check which of the options work 
    self.present(modalVC, animated: true, completion: { 

    }) 
+0

私はこのコードスニペットをどこに置くのですか? –

0

あなたはUITabBarControllerDelegateに準拠していると仮定すると、あなたが実現することができます。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { 

    // here, you should edit "0" to be matched with your selected item 
    // for instance, if there is 5 items and the desired item is in the middle, the compared value should be "2" 
    if tabBarController.selectedIndex == 0 { 

     // simply, you will need to get the desired view controller and persent it: 
     let desiredStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let desiredViewController = desiredStoryboard.instantiateViewController(withIdentifier: "storyboard id") 

     present(desiredViewController, animated: true, completion: nil) 

    } 
} 
+0

私はストーリーボードで直接segueを作ったので、タブバー項目は自動的に作成されます。だから私はすでにタブバーコントローラに接続されたビューを持っていますが、別のビューに切り替えたくないので使用したくありません。私はそれがポップアップしたい。私は何をすべきか? –

+0

詳細を教えてください。 –

+0

Goオリジナルの投稿を確認してください。私の問題は、ビューをモーダルに表示するのではなく、TabBarControllerのためにビューに切り替えることです。私はこれをこのように実装したのでそうしますが、今私はビューを切り替えないでモーダルで表示する方法を尋ねています。それが私の実際の見解よりも飛び出すように。 –

関連する問題