SearchBar in UINavigationItem will not be first controller - ios

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

Related

iOS - Keep ScopeBar even after Cancel Button click

I set the setShowsScopeBar to TRUE from the very beginning, and that works perfectly. But once I search something and then cancel my search, the Scope Bar disappears.
How to keep scopebar even after pressed Cancel button?
Apparently, in earlier version, with the UISearchBar,
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsScopeBar:YES];
return YES;
}
This worked. But with the SearchController, it isn't the case anymore.
I have tried this:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchController.searchBar.showsScopeBar = YES;
[searchController.searchBar sizeToFit];
isSearching = NO;
}
with no luck.
Are there any possible solutions out there on iOS 9?
There is no direct way of keeping the scope bar always visible unless you use some hacks.
Instead use a UISegmentedControl which looks like a scope bar. Then handle the filtering of the search results in
- updateSearchResultsForSearchController: based on the selected segment.
The element is a UISegmentedControl with the UISegmentedControlStyleBar style. You can set the tintColor property to get the color desired. Just put the view above the table view and you can get something that looks like that screenshot.

Why does UIKeyboard appear when press cancel button on UISearchbar?

I am using following code for adding UISearchBar in iOS7 and UISearchBar appearance is fine.But problem is when press cancel button on UISearchBar then UIKeyboard appeared.Why its happen?Please help me.
CGRect searchFrame = CGRectMake(self.view.bounds.origin.x, 64, self.view.bounds.size.width, 44.0f);
self.mySearchBar = [[UISearchBar alloc]initWithFrame:searchFrame];
self.mySearchBar.delegate = self;
self.mySearchBar.searchBarStyle = UISearchBarStyleDefault;
self.mySearchBar.placeholder = #"search items;
self.mySearchBar.showsCancelButton = YES;
[self.view addSubview:self.mySearchBar];
Implement the searchBarCancelButtonClicked: UISearchBarDelegate method. If you want the keyboard to go away you would do something like:
- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
I got your issue now.
When there is not text entered at that time cancel button will be disabled. So if you try to press it than it will consider SearchBar text area & Keyboard will appear.
While you enter the text than Cancel button will be active & than it will work.
Hope you'll get the solution from this.

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.

How to show/hide a search bar inside a navigation bar (iOS 7) as in Apple's Calendar app?

I want to use a search bar button in a navigation bar to show a search bar just like Apple do in the Calendar app. The cancel button would dismiss the search bar and return the navigation bar to its former state, bar buttons, title, etc.
Using the iOS 7 property:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
puts the search bar into the navigation bar just fine. My problem is trying to have it appear conditionally on the press of a bar button. I've tried firing this line from my button's action but no go. I've tried setActive:Animated: on the searchDisplayController
self.searchDisplayController.active = YES;
but no luck either. Any ideas or help would be appreciated.
I'm not sure if you notice, but on the Apple's calendar app when you press the search icon, it open a new UITableView with search bar. If this is what you want to do, you will have a create a UIViewController with a UITableView and a UISearchBar which inside that tableView you will be filtering the content.
If I was you, I will just hide the UISearchBar and call it whenever is needed with the button to show up.
This might work as well. Just give it a try and let me know:
In your viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
// scroll search bar out of sight
CGRect newBounds = self.tableView.bounds;
if (self.tableView.bounds.origin.y < 44) {
newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
self.tableView.bounds = newBounds;
}
// new for iOS 7
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:0 animated:YES];}
Now that is hidden, call this to hide it again when the search is done:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self viewWillAppear:YES];
}
and to show the searchBar with a button, then:
- (IBAction)displaySearchBar:(id)sender {
// makes the search bar visible
[self.searchBar becomeFirstResponder];
}
In iOS 8 you can simply present a UISearchController and you will get the same animation as Calendar.app. Check out Apple's UICatalog sample code for an example.

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

Resources