问题
I have an application that has a UITabBarController
in a UINavigationController
. (The UITabBarController
is not the "Initial View Controller" for the story board.)
In the initial UIViewController
, I have a UISearchController
which allows for content to be searched from a remote source.
This worked fine until I added the UITabBarController
. Now, the search bar displays fine in the default state, but once you start typing inside of it, it gets pushed up past the top edge of the view.
I realize this probably has a lot to do with automaticallyAdjustsScrollViewInsets
, extendedLayoutIncludesOpaqueBars
, and/or edgesForExtendedLayout
, but in all my tweaking, I've been unable to find the magic combo to get the UISearchController
to not push itself up past the top of the view.
Any ideas on how to make it work?
See these screengrabs from this example project:
Initial view:

Search text box becomes first responder:

After typing a word and hitting enter:

回答1:
I ran into this issue too, and here's a solution: setting definesPresentationContext = true
on UITabBarController
instead of the table view controller seems to fix it.
Apparently, you have to go all the way up until navigation controller in container hierarchy. Not the most obvious thing in the world if you ask me.
回答2:
Although definesPresentationContext
worked for me previously, since iOS 10 at some point I had to disable Adjust Scroll View Insets on my search results view controller. More on my answer at UISearchBar gets cut off when using a UINavigationController inside a UITabBarController
来源:https://stackoverflow.com/questions/29979628/search-bar-gets-cut-off-when-using-uisearchcontroller-with-a-uitabbarcontroller