Difference between UISearchController.searchResultsController?.view.isHidden and UISearchController.showsSearchResultsController

huangapple go评论55阅读模式
英文:

Difference between UISearchController.searchResultsController?.view.isHidden and UISearchController.showsSearchResultsController

问题

以下是您要翻译的部分:

"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."

英文:

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.

答案1

得分: 1

使用showsSearchResultsController属性是更好的方法。它是由UISearchController提供的公共API的一部分。设置该属性允许搜索控制器根据需要显示/隐藏搜索结果,这可能涉及远不止简单地显示/隐藏一个视图。

当您像这样做searchResultsController?.view.isHidden = someBool时,您正在深入搜索控制器的(可以说是)私有结构。您绕过了公共API。使用showsSearchResultsController提供的公共API可能会丧失一些细微之处,这些细微之处通过使用showsSearchResultsController获得。

iOS的未来版本可能会更改UISearchController和搜索结果视图的外观和感觉。通过使用公共API,您的代码应该继续正常运行。直接深入搜索控制器视图的内部可能会导致不正确的行为或糟糕的用户界面。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月7日 00:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定