I'm currently working with UISearchController
and I've found out some awkward behavior of UISearchController
.
Previously I didn't know there was the showsSearchResultsController
property which helps me to hide searchResultsController
. So I've tried to manage it by using searchResultsController?.view.isHidden
. But I've found out that there are infrequent cases where I assigned true
to searchResultsController?.view.isHidden
and the view doesn't get hidden.
However, when I used showsSearchResultsController
and assigned false
to it, the searchResultsController
acted as I wanted.
The first code is the code that I used at first (malfunctioned).
Second code is the code that I fixed (worked just fine).
isHidden
.asDriver(onErrorJustReturn: true)
.drive(onNext: { [weak self] in
print("isHidden Before \($0) \(self?.searchController.searchResultsController?.view.isHidden)")
self?.appSearchController.searchResultsController?.view.isHidden = $0
print("isHidden After \($0) \(self?.searchController.searchResultsController?.view.isHidden)")
})
.disposed(by: disposeBag)
isHidden
.asDriver(onErrorJustReturn: true)
.drive(onNext: { [weak self] in
print("isHidden Before \($0) \(self?.searchController.searchResultsController?.view.isHidden)")
self?.appSearchController.showsSearchResultsController = !$0
print("isHidden After \($0) \(self?.searchController.searchResultsController?.view.isHidden)")
})
.disposed(by: disposeBag)
In both cases the output of print was
isHidden Before false Optional(false)
isHidden After false Optional(true)
By using showsSearchResultsController
I've fixed the problem, but I really want to know the difference between two. In my point of view I think two codes look similar. Sharing ideas would be very helpful to me.
Using the showsSearchResultsController
property is the far better approach. It's part of the public API provided by UISearchController
. Setting that property allows the search controller to do whatever it deems as appropriate to show/hide the search results. This likely involves much more than simply showing/hiding a view.
When you do something like searchResultsController?.view.isHidden = someBool
, you are digging into the (arguably) private substructure of the search controller. You are bypassing the public API. You likely lose some nuances or finesse that is gained by using the public API provided by showsSearchResultsController
.
A future version of iOS may change the look and feel of UISearchController
and the search results view. By using the public API your code should continue to function correctly. Digging into the guts of the search controller's view directly could result in incorrect behavior or a bad user interface.