ios UISearchDisplayController dismiss when scroll result table view - ios

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];

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.

How to show "Search Display Controller" using a button

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.

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).

UITableView does't scroll after go to another viewcontroller from searchbar result table and come back

I have a UITableViewController that show data in cells. In viewWillAppear I add a UIView to tableHeaderView with this method self.tableView.tableHeaderView =headerView;
In headerView I add a UISearchBar and a UISegmentControl programmatically. I set UISearchBar delegate like this
self.searchDisplay.delegate = self;
self.searchDisplay.searchBar.delegate = self;
self.searchDisplay.searchResultsDataSource = self;
self.searchDisplay.searchResultsTableView.delegate = self;
the searchDisplay is UISearchDisplayController.
The search is work perfectly but my problem is when I select one cell from searchResultsTableView and go to another viewController when touch back button in UINavigationController and come back to this view the table doesn't scroll anymore.
I add this two method in viewDidLoad and viewWillAppear but still doesn't work
self.tableView.bounces = YES;
self.tableView.scrollEnabled = YES;
It's work perfectly when I select one row from table without search and go and come back.
What's the problem ?
You are actually using two tableviews on the view, (self.tableView and self.searchDisplay.searchBarTableView) one for showing data, and one for showing the searched data. I think the searchViewController is staying on the screen after the segue.
There's a trick to show the whole data and search results in a single tableView (no need to use UISearchDisplayController)
Just do the following to show the search results:
self.tableView.dataSource = self;
self.tableView.delegate = self;
for the actual data, you can empty the UISearchBar and load the whole data in table view datasource.
Try placing your
self.tableView.bounces = YES;
self.tableView.scrollEnabled = YES;
in - (void) viewWillAppear:(BOOL)animated instead of viewDidLoad. I believe viewDidLoad doesn't get called when coming back from a other view, only when being created.

Resources