How to show "Search Display Controller" using a button - ios

I have a weird requirement, or at least something I have never done before... I have a UITableView, the header of that table is a UISearchBar. If you tap on that searchbar the "Search Display Controller" shows up and let you search... But now I need to remove that searchbar and call the "Search Display Controller" from a button.
The problem is that if I remove the SearchBar when I call (from the button):
[self.searchDisplayController setActive:YES animated:YES]
I can't see the UISearchBar on top of the View.
I have no idea how to fix this... I have already tried:
self.tableView.tableHeaderView = nil in the viewDidLoad and when touching the button I set the UISearchBar as the table header again... it looks ugly and the animation is not smooth at all.
hide the searchbar and show it when the button is pressed. (i get an empty space in the tableview's header) and the animation is really bad, because the UISearchBar gets first displayed in the header and the it's moved up to the top of the view...
Any help will be more than welcome!!
Thanks!

I realise that you have tried that, but thats the way we do it and it works:
We add the UISearchBar into the view (not the table view). That being said you can't do that if you are using a UITableViewController. You have to add the tableView to a UIViewController. Then we link it via an Outlet to the Controller and then set it to hidden in viewDidLoad:
self.searchBar.hidden = YES;
Then create a selector to show the searchBar again:
- (IBAction)search:(id)sender
{
self.searchBar.hidden = NO;
[self.searchBar becomeFirstResponder];
[self.searchDisplayController setActive:YES animated:YES];
}
Hope that helps.

Related

UITableView inside childviewcontroller not receiving all taps

I've got a UITableview that displays the search results of a UISearchController. They are inside of a Childviewcontroller.
I write the text on the textfield of the parentviewcontroller and it passes the text to the searchbar of the child view controller.
This all works fine.
But for some reason, when I am choosing a result in the child view controller's tableview, it is not very responsive.
After typing in the search text in the textfield (having the textfield as the firstResponder), most of the times I have to tap more than once to select a row.
(P.S. userInteraction is enabled, otherwise no touch would ever go through.)
Any idea why?
I have same problem, and my solution is:
- (void) displayContentController: (UIViewController*) content{
[content.view setFrame:recorderView.bounds];
UINavigationController *childNavController = [[UINavigationController alloc] initWithRootViewController:content];
childNavController.toolbarHidden = NO; // if you show toolbar
childNavController.view.frame = content.view.frame;
[self addChildViewController:childNavController];
[recorderView addSubview:childNavController.view];
[childNavController didMoveToParentViewController:self];
}
content is my subViewController
Is this for Swift or for Objective-C? Also are you placing a UI Tap Gesture Recognizer on the views that you wish to be touchable?

UISearchBar subview strange behaviour

I have added the UISearchDisplayController via Interface Builder, and then I add the UISearchBar to my navigationBar like this
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
It works and looks fine. What I then want to do is add a subview to the searchBar (to act as a placeholder image). I do it by
[self.searchDisplayController.searchBar addSubview:self.imageView];
The problem is, the imageView is not visible. I NSLog its superview and I get a confirmation that it's the searchBar. I can also see that the searchBar has 1 subview, but when I log the subview, it tells me that it's... drum-rolls... the searchBar itself.
How can this be?
EDIT
Interestingly, if I add the searchBar to navigationBar as a subview and the add my imageView, it's visible. The problem is only when the searchBar is being added to navigationBar by self.searchDisplayController.displaysSearchBarInNavigationBar = YES.
Here's how I solved it - I hope this helps someone. The strange thing about adding a searchBar to navigationBar like this
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
is that the 'didLayoutSubviews' is called twice in the viewController, i.e. the searchDisplayController adds the searchBar to the navigationBar with a delay that I was not able to capture through existing delegate methods.
What I ended up doing instead is setting the zIndex of my imageView to maximum
self.imageView.layer.zPosition = MAXFLOAT;
so even if the searchDisplayController adds the searchBar to the navigationBar with a delay, the imageView still stays on top.

ios UISearchDisplayController dismiss when scroll result table view

I used UISearchDisplayController to implement Search function. My main controller A has a tableview and I added UISearchBar to its tableheaderview.
I assigned:
searchDisplayController.searchResultsDatasource = controllerA;
searchDisplayController.searchResultsDelegate = controllerA;
When I click on uiSearchBar, it show searchDisplayController as normal, and search works right, no problem. But when I scroll resultTableView on searchDisplayController, it dismiss searchDisplayController and return to controller A. Why I can't scroll?
As I thinking, searchDisplayController use tableView of controller A, and when I scroll, searchDisplayController is auto resign active. How can I don't allow searchDisplayController resign active?
Maybe you make the same mistake with me. when we scroll the searchResultsTableView, this delegate method -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar will be called.
So if you put the line below into the method, that'll be happened just like you said.
[self.searchDisplayController setActive:NO animated:YES];

SearchBar in UINavigationItem will not be first controller

I have a UISearchBar in my UINavigationItem like so:
searchBar = [[UISearchBar alloc] init];
[searchBar setDelegate:self];
self.navigationItem.titleView = searchBar;
Then in my table view I use:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.searchBar resignFirstResponder];
}
--so that the keyboard will disappear if the user scrolls through the results.
However, now when I click on the search bar again, I can't get it to pull up the keyboard. I figured the view would do it automatically but it doesn't (clearly). I tried:
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar becomeFirstResponder];
}
It didn't work (and I didn't expect it to)
searchBarTextDidBeginEditing is not going to be called until after the search bar becomes first responder, so clearly your becomeFirstResponder call is not even being called.
I am just as surprised as you are that the search bar does not just automatically become first responder when it is tapped. Is some other code (not shown above) disabling it? Did you implement searchBarShouldBeginEditing: to return NO?
EDIT: After chat, turned out the problem was that he was not assigning the UISearchBar a frame (as you can see from his code).

Cannot get UISearchBar Scope Bar to appear in Toolbar (or anywhere) on iPad

This is really causing me fits. I see a lot of info on putting a UISearchBar in the top row of a UITableView -- but I am putting the UISearchBar into the Toolbar at the top of my screen (on the iPad). I cannot find ANYTHING regarding how to handle UISearchBar and UISearchDisplayController using a UIPopoverController on the iPad. Any more info about the UISearchDisplayController using a UIPopoverController would be greatly appreciated. Please help with this as I am at my wit's end.
Using IB, I put a toolbar on the IUView on the iPad. I added the following: Search Bar (not Search Bar and Search Display) to the toolbar. I set the options to be as follows: Show Cancel Button, Show Scope Bar, Scope Button Titles are: "Title1" and "Title2" (with Title2's radio button selected). Opaque, Clear Context and Auto Resize are checked. I hooked up the delegate of Search Bar to the "File's Owner" and linked it to IBOutlet theSearchBar.
In my viewWillAppear I have the following:
//Just in case:
[theSearchBar setScopeButtonTitles:[NSArray arrayWithObjects:#"Near Me",#"Everywhere",nil]];
//Just in case (again):
[theSearchBar setShowsScopeBar:YES];
//doesn't seem to do anything:
//[theSearchBar sizeToFit];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:theSearchBar contentsController:self];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
//again--does not seem to do anything..but people have suggested it:
[theSearchBar sizeToFit];
Okay, so far, I thought, so good. So, I made the File's Owner .m file to be a delegate for: UISearchBarDelegate, UISearchDisplayDelegate.
My issue: I have yet to implement the delegates necessary to do the search but still... shouldn't I be seeing the scopeBar next to the search field when I click into the search field? Just so you know I DO see the log of the characters I type, so the delegate is working.
Here is a routine I used to check to see if IB really put the Scope Bar (UISegementedControl) in the searchbar:
for (UIView *v in theSearchBar.subviews)
{
if ([v isMemberOfClass:[UISegmentedControl class]])
{
// You've got the segmented control!
UISegmentedControl *scope = (UISegmentedControl *)v;
// Do your thing here...
NSLog(#"Found Scope: '%#'\n",scope);
NSLog(#"Scope Segments: '%d'\n",[v numberOfSegments]);
}
}
This shows:
[30013:207] Found Scope: '<UISegmentedControl: 0x68a06b0; frame = (0 0; 200 44); opaque = NO; layer = <CALayer: 0x68a0600>>'
[30013:207] Scope Segments: '2'
So, I know the 2 segments are there. I also know they are not showing up...
What am i doing wrong?
Why doesn't the Scope Bar appear? A results UIPopoverController appears with the title "Results" and "No results found" (of course) when i type the first character in my search...but no scope bar. (not that i expect anything other than "No Results Found".
I am wondering where the scope bar is supposed to appear...in the titleView of the UIPopover? In the toolbar to the right of the search area? Where?
[self.searchDisplayController searchBar] setScopeButtonTitles:[NSArray arrayWithObjects:#"Near Me",#"Everywhere",nil]];
This Might Help.
UISearchBar does not display it's scope when embedded in a UIToolbar, this is true even in iOS 7.
You must build your own UISegment and add it into the table view of the search results or build your own view to hold the UISearchBar that would display scope.

Resources