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.
- private func configSearchControll() {
- let sc = UISearchController(searchResultsController: nil)
- // config search controller
- if #available(iOS 16.0, *) {
- sc.scopeBarActivation = .onSearchActivation
- } else {
- if #available(iOS 13.0, *) {
- sc.automaticallyShowsScopeBar = true
- } else {
- // Fallback on earlier versions
- }
- }
- }
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
.
- override func viewDidLoad() {
- super.viewDidLoad()
- // some configuration codes
- DispatchQueue.main.async {
- [weak self] in
- guard let this = self else { return }
- this.configSearchController()
- }
- }
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
- Apple Developer: Instance Property automaticallyShowsScopeBar (deprecated)
- My post soltion to a question at SO: Swift 4 - iOS 11 Search bar scope won't appear as it should
- My iOS productivity app: TapMyNotes
0 comments:
Post a Comment