Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

iphone - How to keep scopebar even after pressed Cancel button?

I have a UITableView with a searchbar on the top. I used UISearchDisplayController for implementing the same. And also it has a scope bar with two buttons. In default when I launch the app, the scope bar will be displayed. When I click the cancel button after the searching, the scopebar disappeared. So is there any way to keep the scopebar even after I pressed the Cancel button. I used the following code but its not working.

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsScopeBar:YES];
    return YES;
}

Thanks :)

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I've had the problem today and I think I've found a solution.

You need to do two things:

  1. Call 'sizeToFit' on the searchBar after setting showsScopeBar. This will ensure the searchBars frame is set correctly to include the scope bar.
  2. Unfortunately the table view doesn't seem to like it when the searchBar resizes and causes the scope bar to overlap the first cell. To solve this you can re-set the tableHeaderView to be the searchBar (again) which seems to fix the overlap problem.

Final code:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsScopeBar = YES;
    [self.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchBar;
    return YES;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...