简体   繁体   中英

Search bar in iOS7 half hidden when setting content offset

I have a table view controller inside a navigation controller, and on viewDidLoad I add some views to the table view header :

//Build up master container view with search bar and another view that has buttons
...

//Set this as the table header
self.tableView.tableHeaderView = masterHeaderContainerView;

If I do nothing else at this point my view from top to bottom looks like

Navigation bar, Search bar, Action bar with buttons, Table View cells... https://www.dropbox.com/s/mwhipz5qpa9duzw/filterAndActions.PNG

However, I want to hide the search bar under the navigation bar, so I set the content offset of the table view in viewWillAppear

self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);

On iOS 6 this worked fine, but on iOS 7, when I then scroll down to reveal the search bar, it's vanished, there's a blank space where it should be.

What might give a clue to the problem is that if I hard code the content offset to 22 (half the height of the search bar), and then scroll down, the search bar has been half hidden by what looks like the background.

https://www.dropbox.com/s/jm1vro769jfhpy8/halfFilterBar.PNG

So the amount by which I set the content offset is the amount by which the search bar is covered. Any ideas what might be going on here?

The goal is to have a filter bar like the Mail app, that's hidden under the navigation bar when the view first appears, but can be revealed by scrolling down.

I'm building the search bar and search display controller programmatically, as the view has no xibs / storyboard, it's a standard UITableViewController subclass.

UISearchBar *aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
UISearchDisplayController *aSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self];
aSearchBar.frame = ...

您应该设置为在iOS 7中获得预期的行为

self.edgesForExtendedLayout = UIRectEdgeNone;

add this

 [self.view bringSubViewToFront:aSearchBar];

also

[self.tableView setClipsToBounds:YES];

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