LoginSignup
25

More than 5 years have passed since last update.

UISearchControllerとpushSegueを同時に使用する際の問題解決

Last updated at Posted at 2016-07-03

問題について

UISearchControllerで表示した結果から別画面に遷移する機能を実装していた。

下記動画のようになってしまい遷移をするたびにTableViewがずれるようになってしまった。

問題定GIF.gif

この問題の解決法は簡単で、UISearchControllerを使っている遷移元ViewcontrollerのviewDidLoad内で
definesPresentationContext = trueと一行追加すれば良い。

問題解決.gif

上記のように解決する。

蛇足

何故definesPresentationContext = trueの追加で解決するかを解説。

definesPresentationContext について

definesPresentationContextというプロパティについて軽く説明する。
このパラメータは、UIViewcontrollerにおけるPresentationContextを制御するプロパティである。
PresentationContextは、ViewControllerをモーダル表示するときに、Viewcontroller階層の中で、どのViewControllerを基準に表示するかを決定する際に使用される。
デフォルトはfalseなので、自分で特別に設定していなければ、UIWindowやUINavigationControllerなどがモーダル表示の起点となる。

原因について

UISearchControllerが結果を表示する時に、definesPresentationContextに影響を受けている。
そのため、デフォルトの状態だと、表示の起点となるViewControllerはUINavigationcontrollerとなってしまい、検索結果が遷移先のViewControllerを覆い隠すように表示されてしまっている。
それによって、正常な処理がブロックされ、問題が生じているものと推測される。

本来であれば、遷移元ViewController内で行われるべき検索結果の表示が、さらにその親ViewControllerで行われてしまっていたのが、今回の問題の原因。

遷移元ViewControllerに対して、検索結果の表示が行われるようにするために、definesPresentationContextの設定を行った。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25