IOS7: uisearchdisplaycontroller always shows the visibility bar

Basically, I am trying to ensure that my visibility panel does not disappear.

Environment: iOS 7, storyboard, inside the view controller. I have a "search bar and display controller" and a separate table view (the search bar is not inside the table).

Inside the controller view.h

@property (nonatomic, strong) IBOutlet UISearchBar *candySearchBar; 

Inside the .m view controller

 @synthesize candySearchBar; 

What I tried: inside a custom search class class

 - (void) setShowsScopeBar:(BOOL) showsScopeBar { if ([self showsScopeBar] != showsScopeBar) { [super invalidateIntrinsicContentSize]; } [super setShowsScopeBar:showsScopeBar]; [super setShowsScopeBar: YES]; // always show! NSLog(@"setShowsScopeBar searchbar"); NSLog(@"%hhd", showsScopeBar); } 

and

 searchBarDidEndEditing 

Same thing in the view controller, but then

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [candySearchBar setShowsScopeBar:YES]; [candySearchBar sizeToFit]; } 

I hope my question is clear, I tried many solutions posted all over the Internet, most of them talk about the setshowsscopebar set, but it doesn't seem to work. The log output in setshowscopebar is 1, but the scope is still not displayed.

I still consider myself a novice in the code, an error can still be a novice error.

edit: another piece of code in the view controller, as you can see that I'm looking for a blind:

 -(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{ self.searchDisplayController.searchBar.showsCancelButton = YES; self.searchDisplayController.searchBar.showsScopeBar = YES; controller.searchBar.showsScopeBar = TRUE; controller.searchBar.frame = CGRectMake(0, 149, 768, 88); UIButton *cancelButton; UIView *topView = self.searchDisplayController.searchBar.subviews[0]; for (UIView *subView in topView.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) { cancelButton = (UIButton*)subView; } } if (cancelButton) { //Set the new title of the cancel button [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; [cancelButton setEnabled:YES]; controller.searchBar.showsScopeBar = YES; //candySearchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags", @"Listeners", @"Stations", nil]; } NSLog(@"%@",NSStringFromCGRect(controller.searchBar.frame)); NSLog(@"%@",NSStringFromCGRect(controller.searchBar.bounds)); NSLog(@"% hhd@ ",controller.searchBar.hidden); } 
+1
source share
4 answers

The code you tried will not work in iOS7 because the apple changed its UISearchBar behavior to hide the area when it returns to normal view. Add this method to your own 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]; } } 

Direct access to an object in the index can cause the application to crash in iOS6 due to differences in the hierarchy of views between iOS6 and iOS7, to avoid this, add this inside if the condition is only in case of iOS7.

In addition, this is also required in the custom search class class.

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

I have the same problem. Perhaps this is something that has changed in iOS7, since displaying the visibility bar by default is a behavior. You can check this in the โ€œCreating an Additional Visibility Panel to Filter Resultsโ€ section in the following tutorial:

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

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

0
source

initialize the value set string NO

 [self.searchBar setShowsScopeBar:NO]; [self.searchBar sizeToFit]; //default scope bar selection self.searchBar.selectedScopeButtonIndex=3; 

uncheck / uncheck the scopeBar field

enter image description here

0
source

It is possible (but hacked) to do this without a custom searchBar, which is pretty similar to what CoolMonster offers.

In your TableViewController, this will show the ScopeBar after the search is complete:

 - (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 the search, add this line to the TableViewController viewDidLoad:

  [self searchDisplayControllerDidEndSearch:self.searchDisplayController]; 

For the record, after you made it, I ended up using a separate segmented control instead of the approach above for several reasons, not least from the ScopeBar SearchBar, as soon as you display it, it starts the displayView display table, which has meaning if you use it in the recommended way. However, since I wanted ScopeBar to work without starting the search table, it made more sense to me to just use my own segmented control and add it to my tableHeaderView under the SearchBar.

0
source

All Articles