简体   繁体   中英

iOS13 - UITableView + UISearchController with custom hidesSearchBarWhenScrolling behavior

On iOS 13, if you set hidesSearchBarWhenScrolling to true, the view controller will hide the search bar, and you have to scroll down to reveal it. If you set it to false then it is immediately shown and it won't disappear.

I think this is a reasonable behavior, assuming that the end users are all versed in the way the Apple products work. Given that's not seem to be the case, I would like to show the searchbar at first visit, but then if the user starts scrolling up, the search bar is hidden. So this is a mixed behavior of the true/false.

This worked on iOS12, where I set the hidesSearchBarWhenScrolling = true in the viewDidAppear , but now on iOS 13 it's not the case. The table scrolls, but the searchbar stays on top instead of scrolling it together with the tableview(btw this is only because the screen is not rendered again).

Any idea how to go about it? I tried changing the content offset of the tableview, but no luck really.

For me it worked after adding following lines in viewDidLoad() method:

navigationController!.navigationBar.sizeToFit()

No need to specify hidesSearchBarWhenScrolling as I think by default it is true , but if you still see the search bar then you can set it true to hide it.

I faced the same issue as you and looks like such issue appears in iOS 13 only. In iOS 14 your solution works perfectly. To make it work in iOS 13 you need to add

self.navigationItem.hidesSearchBarWhenScrolling = true
self.navigationController?.view.setNeedsLayout() // add this line

in viewDidAppear

It works for me in simulator iOS 13.7 I hope it work for you as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM