@@ -6,8 +6,10 @@ class ViewController: UITableViewController {
6
6
7
7
override func viewDidLoad( ) {
8
8
super. viewDidLoad ( )
9
-
9
+ definesPresentationContext = true
10
10
searchController. searchBar. scopeButtonTitles = [ " Scope 1 " , " Scope 2 " ]
11
+ searchController. searchBar. searchBarStyle = . minimal
12
+ searchController. obscuresBackgroundDuringPresentation = false
11
13
searchController. searchBar. showsScopeBar = true
12
14
navigationItem. searchController = searchController
13
15
}
@@ -22,5 +24,26 @@ class ViewController: UITableViewController {
22
24
return cell
23
25
}
24
26
25
- }
27
+ // HAX: this hides and shows the scope bar appropriately to allow
28
+ // the system animation of the search bar to complete properly.
29
+ private var lastContentOffset : CGFloat = 0
30
+ override func scrollViewDidScroll( _ scrollView: UIScrollView ) {
31
+ let searchBar = searchController. searchBar
32
+
33
+ if lastContentOffset > scrollView. contentOffset. y {
34
+ // Scrolling down, show the scope bar.
35
+ if !searchBar. showsScopeBar && searchBar. frame. size. height > 44 {
36
+ searchBar. showsScopeBar = true
37
+ }
38
+ } else if lastContentOffset < scrollView. contentOffset. y {
39
+ // Scrolling up, hide the scope bar to allow the searchBar to collapse
40
+ // completely.
41
+ if searchBar. showsScopeBar && searchBar. frame. size. height <= 44 {
42
+ searchBar. showsScopeBar = false
43
+ }
44
+ }
45
+
46
+ lastContentOffset = scrollView. contentOffset. y
47
+ }
26
48
49
+ }
0 commit comments