Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

iphone - Sticky search bar and section header behavior similar to native Contacts app

I'd like to replicate the sticky behavior of the search bar in the iPhone Contacts app.

Normal stage

When the user scrolls the view down, the search bar also comes down along with the view:

User scrolls the view down

If the user scrolls up, the table scrolls accordingly, with the following two behaviors:

(1) the search bar remains fixed at the top, and
(2) the subsequent section header stops appropriately below the search bar:

User scrolls up

When the next section header comes, the previous header disappears below the search bar:

User scroll up

Note: the section index control (a-z on the right hand side) appears on top of the search bar as well. Ergo, fiddling with the contentInset will push the section index control down along with it.

I've created a custom UIViewController, added a UITableView, set its contentInset to the height of the search bar. I created a UIView, added the search bar as its subview, and then added the UIView to the UITableView. However, as noted above, when the user is scrolling, the section headers still stick at the y-position zero, and not at the header height. Additionally, the section header index control position is adversely affected.

I'd appreciate a solution to this problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It has been quite some work to get all things right, but I just had to prove that it's possible to recreate that behavior, at least almost.
Check out this GitHub project I've created: https://github.com/fabiankr/TableViewSearchBar

Actually, it's not even that complicated:
1) Add the search bar directly to the table view and set the tableView's contentInset
2) In -scrollViewDidScroll: adjust the search bar's frame

There are some caveats you have to take care of, though:
1) When scrolling the table view to the top, the section headers shortly appear above the search bar. In order to solve it, set the search bar's zPosition when scrolling to the top
2) Your content controller needs to be a subclass of UIViewController instead of UITableViewController, because UISearchDisplayController adds the dimming view to the controller's view. Because table view controllers' viewis a table view, the dimming view would be at the wrong position when the table view is scrolled.

One thing that isn't possible using public API only is to make the section index control on the right of the table overlap the search bar. It's only a minor thing and even without it the behavior is very similar to the one found in the Contacts app.

In order to achieve the exact same behavior, you'll have to use private API. There's a method on UITableView called _setPinsTableHeaderView: that needs to be used. The sample project contains implementations for both: 1) public API only and 2) private API to get the section index control overlap the search bar.
Reminder: You shouldn't use private API when you plan to submit the app to the App Store!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...