IOS7 : uisearchdisplaycontroller always show scope bar

雨燕双飞 提交于 2019-12-04 18:36:28
CoolMonster

The code you tried will not work in iOS7 onward because apple has changed it behavior of UISearchBar to hide the scope when return to normal view. Add this method to your custom searchBar class.

-(void)layoutSubviews
{
    [super layoutSubviews];
    if([[UIDevice currentDevice].systemVersion floatValue]>=7.0) {
         //Get search bar with scope bar to reappear after search keyboard is dismissed
         [[[[self.subviews objectAtIndex:0] subviews] objectAtIndex:0] setHidden:NO];
         [self setShowsScopeBar:YES];
     }
}

Directly accessing object at index may crash the app in iOS6 because of difference in view hierarchy between iOS6 and iOS7, to avoid this, add this inside if condition only when its iOS7.

In addition this is also required in the custom search bar class

-(void) setShowsScopeBar:(BOOL)showsScopeBar {
    [super setShowsScopeBar:YES]; //Initially make search bar appear with scope bar
}

I have the same issue. Perhaps it is something that has changed in iOS7 since showing the scope bar is supposed to be the default behaviour. You can verify this in the section "Creating an Optional Scope Bar to Filter Results" of the following tutorial:

http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view

Hopefully someone has a solution for this; otherwise we will have to look for a workaround.

initialize set scope bar NO

[self.searchBar setShowsScopeBar:NO];
[self.searchBar sizeToFit];

//default scope bar selection
self.searchBar.selectedScopeButtonIndex=3;

unselect/remove tick from scopeBar checkbox

It's possible (but hacky) to do this without a custom searchBar, in a pretty similar way to what CoolMonster suggests.

In your TableViewController, this will show the ScopeBar after a search ends:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    //Show the scopeBars
    controller.searchBar.showsScopeBar = YES;

    //Resize the searchBar to show ScopeBar
    controller.searchBar.frame = CGRectMake(0, 0, 320, 88);

    if([[UIDevice currentDevice].systemVersion floatValue]>=7.0) {
        [[[[controller.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] setHidden:NO];
    }
}

Then, since you probably want it to appear before you search, add this line to the TableViewController's viewDidLoad:

    [self searchDisplayControllerDidEndSearch:self.searchDisplayController];

For the record, after getting this to work, I ended up using a separate segmented control instead of the approach above for several reasons, not least of which was that touching the ScopeBar of a SearchBar, once you get it to display, launches the search display tableView, which makes of sense if you're using it the recommended way. However, since I wanted the ScopeBar to work without launching the search tableview, for me it made more sense just to use my own segmented control and add it to my tableHeaderView under the searchBar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!