2017-10-23 3 views
0

検索バーのようないくつかのUI要素を、私のアプリケーションの最初のVCの上に表示し、それが提示する2番目のVCの上に表示したいと思います。UIViewControllerとその提示されたViewControllerの上にUI要素を保持するにはどうすればよいですか?

私の解決策は、addChildViewController(firstViewController)view.addSubview(firstViewController.view)を呼び出すContainerViewControllerを作成することでした。そしてview.addSubview(searchBarView)、そして各UI要素について同様です。

ある時点で、FirstViewControllerはpresent(secondViewController)を呼び出すことができます。理想的には、検索バーと他の要素が両方のビューコントローラの上に表示された状態で画面上にスライドします。

代わりに、secondViewControllerがContainerViewControllerの上部に表示されるため、検索バーは表示されません。

ユーザが検索バーをタップすると、ContainerViewControllerがSearchVCをすべての上に表示することも欲しい。そのためには、それは簡単です - containerVC.present(searchVC)

この階層を正しく機能させるにはどうすればよいですか?

+1

は良い方法を探します。ちょうどあなたが 'present(secondViewController)'を使ったのと同じようにsecondVCを遷移させるカスタムビューのアニメーションやナビゲーションベースのトランジションを使うことができます。この方法で一番上のUI要素は隠れません。 – iAviator

答えて

0

私が正しく理解していれば、あなたの質問は、親ビューの境界とは異なるフレームを持つ子ビューコントローラの上(境界内)に表示​​コントローラを表示する方法です。これは、表示するコントローラのmodalPresentationStyle.overCurrentContextに設定し、子ビューコントローラのdefinesPresentationContexttrueに設定することで可能です。ここで

は、それが実際にどのように動作するかを示す簡単な例です:containerviewcontrollerを使用して

override func viewDidLoad() { 
    super.viewDidLoad() 

    let childViewController = UIViewController() 
    childViewController.view.backgroundColor = .yellow 
    childViewController.view.translatesAutoresizingMaskIntoConstraints = true 
    childViewController.view.frame = view.bounds.insetBy(dx: 60, dy: 60) 
    view.addSubview(childViewController.view) 
    addChildViewController(childViewController) 
    childViewController.didMove(toParentViewController: self) 

    // Wait a bit... 
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { 
     let viewControllerToPresent = UIViewController() 
     viewControllerToPresent.modalPresentationStyle = .overCurrentContext // sets to show itself over current context 
     viewControllerToPresent.view.backgroundColor = .red 

     childViewController.definesPresentationContext = true // defines itself as current context 
     childViewController.present(viewControllerToPresent, animated: true, completion: nil) 
    } 
} 
関連する問題