How to programmatically change scope selected in searchbar? - ios

I want to change the selected scope after user has selected something from a table view.
I have tried this:
for (id view in [self.searchDisplayController.searchBar subviews]) {
if ([view isMemberOfClass:[UISegmentedControl class]])
{
UISegmentedControl *scopeBar = view;
scopeBar.selectedSegmentIndex = 1;
}
}
But the scope button i want is still not selected. My search bar doesn't have a subview of class UISegmentedControl.

I assume you mean the scopes of an UISearchBar and you already have your UISearchBar set up so it shows scopes.
The UISearchBar has a special property that needs to be set:
self.searchDisplayController.searchBar.selectedScopeButtonIndex = 1;

Related

UISearchBar Properties

I am working on a legacy application and can't figure out why a UISearchBar is not showing up correctly.
I am trying to make the background be white or any other color but can't seem to set it correctly. How can I set the background so it's not dark as shown below?
Here is what I have tried:
[self.searchController.searchBar setSearchBayStyle:UISearchBarStyleDefault];
[self.searchController.searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CFImageRef) ([UIColor whiteColor])]];
The search bar is added in programmatically so I can't use the attributes inspector.
Alternatively, is there a way to print what the search bar properties are for style, etc via NSLog?
Every search bar view contains a textfield and here you see background color of this textfield. By default it uses tintColor.
To set manually you need to create a method for recursively finding up textfield and then background color on it.
- (UITextField*)searchSubviewsForTextFieldIn:(UIView*)view
{
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchedTextField;
for (UIView *subview in view.subviews) {
searchedTextField = [self searchSubviewsForTextFieldIn:subview];
if (searchedTextField) {
break;
}
}
return searchedTextField;
}
And then call this method as:
[[self searchSubviewsForTextFieldIn:self.searchController.searchBar] setBackgroundColor:[UIColor whiteColor]];
Hope it will work!
I have created an extension for UISearchBar customisation
Link: https://github.com/Rakshith-Nandish/SearchBarCustomiser
Also to change the UITextField background color you can use this
let textFieldSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.backgroundColor = <your desired color value>

pick up custom views in UIView hierarchy

There is a situation that should select all custom view(not system view type such as UILabel or UIButton etc.) like XXButton or XXView. How can I iterate a UIView's subviews to figure out all the custom views? In other words, how to distinguish between unknown class custom views and Apple system views?
Try Following,
for viw in self.view.subviews
{
if viw.classForCoder == yourCustomViewClass
{
// do your required operation
}
}
In above case first we have used for in loop to iterate on all sub views of particular view.
Then we have checked the class for the view from the subview's array
When you created a XXButton or XXView, they basically inherited from the UIButton and UIView respectively. Thus you have to explicitly checks for your custom class only.
//Loop through all the views in your superview.
for(UIView *anyView in self.view.subviews) {
if([anyView isKindOfClass:[XXButton class]]) {
// It's a XXButton. Need to cast it.
XXButton *btn = (XXButton *)anyView;
} else if([anyView isKindOfClass:[XXView class]]) {
// It's a XXView. Need to cast it.
XXView *view = (XXView *)anyView;
}
// You can multiple else if conditions for your custom UI classes.
}

UISearchDisplayController table view overlapping search bar

I have a UITableViewController subclass, displayed in a modal view on an iPad. The view controller has a UISearchDisplayController subclass with a UISearchBar included in the header view of the table.
The subclassed UISearchDisplayController is called NoAnimationSearchDisplayController and I have overridden the - (void)setActive:(BOOL)visible animated:(BOOL)animated
method to prevent the search bar from animating into the navigation bar when it's set to active. The method override is below...
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if (self.active == visible) {
return;
}
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
The problem I'm having, is that when I have searched, and results are displayed in my search display controller's table view, everything looks fine untill i try to scroll down the list, at this point the content within the cells appears above the search bar, as shown in the following screen:
The search bar is set to UISearchBarStyleMinimal and transparency is enabled. Can anyone let me know how to stop this content overlapping the search bar? Ideally the content would disappear under the search bar as if it was the end of the view.
The answer was to manually change the frame of the table view provided by the UIsearchDisplayController in the appropriate delegate method...
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
/**
* Remove content inset automatically set by UISearchDisplayController as we are forcing the
* search bar to stay in the header view of the table, and not go into the navigation bar.
*/
[tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
/**
* Recalculate the bounds of our table view to account for the additional 44 pixels taken up by
* the search bar, but store this in an iVar to make sure we only adjust the frame once. If we
* don't store it in an iVar it adjusts the frame for every search.
*/
if (CGRectIsEmpty(_searchTableViewRect)) {
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y + 44;
tableViewFrame.size.height = tableViewFrame.size.height - 44;
_searchTableViewRect = tableViewFrame;
}
[tableView setFrame:_searchTableViewRect];
}
Be sure the search bar is not translucent enabled ( on storyboard/xib ) or by code.
Then make the background to white or whatever color you want.

Adding UISearchBar with Scope button as subview to UIView

I want to add UISearchbar with two scoop button to my UIView. i do not want to either navigation or table view.
when user open model view page from sheet comes up with Search option and rest. when user click on Search button scoop button to be visible.
i tried to do but i am not getting desire result.
how to remove Background of scope button, i have removed uisearchbar, similar way i do not want white background for button. is it possible. showsScopeBar i am making FALSE still background can be visible.
To remove Background image of scope buttons just try :
Class segmentedControlBackgroundView = NSClassFromString(#"_UISegmentedControlBackgroundView");
for(UIView *view in self.searchBar.subviews) {
if([view isKindOfClass:[UISegmentedControl class]]){
for(UIView *subview in view.subviews) {
if ([subview isKindOfClass:segmentedControlBackgroundView]) {
subview.hidden = YES;
}
}
}
}

Adding a UISegmentedControl to UISearchBar instead of Scope Buttons

I'd like to add scope buttons below my UISearchBar. However I cannot change the tint color of the built in scope buttons.
Instead, I added a UISegmentedControl to my tableViewHeader. This works well enough, but it only shows when I am not typing into the UISearchbar. Not very convenient.
When I enter text into the UISearchBar, the table and segmented controls become hidden by the "no results shown" semi-opaque black layer. Once results start showing my segmented control disappears altogether, and only cells with results show.
I want to make the segmented control clickable during text entry into the search bar.
Do you know of any way to do the following?
make UISegmentedControl move with UISearchBar when text is being entered, or
show UISegmentedControl whilst search results are displayed on the UITableView
Thank you
try
#implementation UISearchBar (subviewAccess)
- (UISegmentedControl *)scopeBar {
for (UIView *v in [self subviews]) {
if ([v isKindOfClass:[UISegmentedControl class]])
return v;
}
return nil;
}
#end
to get hold of the segmented control you want, and tint it from there
(it's currently at index 0, but that's definitely not for sure)
there is no "private API" being used, so apple should be okay with it,
but note if they changed their view layout (unlikely), this could break,
which would have the side effect of your tint disappearing, you should
access the rest of its state through the standard search bar APIs

Resources