Thursday, March 28, 2024

Scope Not Shown When Search Bar Is Activated

Recently, I noticed a minor issue after updating iOS. The scope bar did not appear when the search bar was activated. Typically, I prefer to display the scope bar simultaneously with the search bar activation. This allows users to select a scope item before searching.

An Example Case

Here is a snapshot. When I pull down the screen, only the search bar is displayed, but the scope bar is missing.


The scope bar appears when text is entered into the search bar. However, if a user selects a scope item, the search text disappears. This means that the user has to re-enter text if they selecte a different scope item. This is very inconvenient.



Solution

I spent some time thinking about the issue before diving into my project. It became apparent that there were some logic or behavior changes in iOS. Soon, I discovered that a property of the search bar had been deprecated and a new property was recommended as its replacement.

  1. private func configSearchControll() {
  2.  let sc =  UISearchController(searchResultsController: nil)
  3.  // config search controller
  4.  if #available(iOS 16.0, *) {
  5.        sc.scopeBarActivation = .onSearchActivation
  6.    } else {
  7.        if #available(iOS 13.0, *) {
  8.            sc.automaticallyShowsScopeBar = true
  9.        } else {
  10.            // Fallback on earlier versions
  11.        }
  12.    }
  13. }

I noticed that the deprecated property of automaticallyShowsScopeBar is a boolean type with only one choice. With the iOS update, the recommented property of scopeBarActivation is an enum type with several options. After further testing, I found that only the enumerator onSearchActivation is meeting my requirements.


The above method is called in the event of viewDidLoad.

  1. override func viewDidLoad() {
  2.  super.viewDidLoad()
  3.  // some configuration codes
  4.  DispatchQueue.main.async {
  5.    [weak self] in
  6.      guard let this = self else { return }
  7.      this.configSearchController()
  8.  }
  9. }

With above code updated, my iOS app TapMyNotes now works as expected!


I'll update my app TapMyNotes on the App Store with this bug-fix, as well as other new and exiting features, soon!

References

0 comments: