问题
Update 2:
Since people are still looking at this question: know that I realized it's impossible to replicate this bug on an actual, isolated iOS device. When you present the search controller, iOS presents a keyboard as well, which covers the tab bar. To switch tabs, you must dismiss the keyboard, which will dismiss the search controller as well.
The reason I say "isolated" is because I don't know if you can replicate this with a Bluetooth keyboard and don't have one to try. The iOS keyboard might not be presented with an external keyboard connected
Update:
I added a viewWillDisappear override to View 1 and found some interesting results:
When the searchbar is not present, switching tabs does dismiss the view before loading the next view. However, when the searchbar is present, the view is NOT dismissed before loading the next view
I have a tab bar controller at the root, which has 2 tabs. Each goes to a navigation controller that takes the device to the view. Here's a crude diagram:
/-> Navigation Controller -> View 1
tab bar controller
\-> Navigation Controller -> View 2
This works fine normally. However, View 1 has a UISearchController. The bug is that if you click the search button, the search controller is presented. If you then click View 2 in the tab bar and switch back to View 1, the search controller is still there and the view is black.
My current hacky "fix" is to disable the tab bar while the search bar is apparent and re-enable it if the user selects "Cancel". This has 2 issues:
- It's jank; the user should be able to switch tabs whenever they want
- It only re-enables if the user hits Cancel, not if they simply tap out of the search
How can I fix this? Here are pictures of what I'm talking about
回答1:
I solved this by following the answer at: UISearchController causes black screen Swift 2.0
I implemented it like so in viewDidLoad
:
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
This pretty much solved the problem for me. Then I encountered another problem with trying to segue away at didSelectRowAt
, which had previously never been an issue. I solved this by first popping the UISearchController, then performing a segue as normal:
_ = self.navigationController?.popViewController(animated: true)
来源:https://stackoverflow.com/questions/38836862/tab-bar-view-goes-blank-when-switched-back-to-with-search-bar-active