Update 03/25/14
It looks like the lack of animation was a bug in iOS 7.0, and has been fixed in iOS 7.1.
Original Question
Normally, when you tap inside a search bar in iOS 7, it has a nice little animation where the magnifying glass and "Search" text slides from the middle of the search bar to the left side. However, if I put the search bar in the navigation bar instead of within a table view, it no longer animates. Instead, the magnifying glass and "Search" text jumps instantly to the left side of the search bar.
You can see this really easily using Apple's UISearchBar sample project. Open up the storyboard. Add a boolean runtime attribute displaysSearchBarInNavigationBar on Search Display Controller, and check the checkbox it creates. Then, move the Search Bar up a level in the tree, so it is outside of the Table View. Run the project, and the search bar should now be in the navigation bar. Note that when you tap in the search bar, it doesn't animate.
Has anyone successfully put a UISearchBar in the navigation bar in iOS 7, and had it animate properly?
To be clear, my problem is that there is no animation of the search bar between the first and second screenshots below, when the user taps on the search bar.
In iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES for to get this working easily.
Apple Inc Doc:
Starting in iOS 7.0, you can use a search display controller with a
navigation bar (an instance of the UINavigationBar class) by
configuring the search display controller’s
displaysSearchBarInNavigationBar and navigationItem properties.
your expected answer
Remove the search bar from the table view. Using displaysSearchBarInNavigationBar means that UISearchDisplayController will take care of placing the search bar in the hierarchy for you.
- (void)viewDidLoad
{
self.searchDisplayController.displaysSearchBarInNavigationBar=YES;
}
This was not animation first time tap but the animation works good in cancel moments:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
Related
I've tried to use all the properties UISearchBar has with different combinations, I've searched for the answer for a while but still can't solve the problem.
Problem:
I have a UIViewController with UITableView in it and some other view in storyboard. I've added Search Bar and Screen Display Controller into my project (search bar is added as a subview/header of the UITableView)
I need to make it red color with no transparency/translucency to match my navigation bar.
I've tried "solution" from How to change background color of UISearchBar in iOS7 but it doesn't work. Result of this solution looks like that:
searchDisplayController?.searchBar.barTintColor = UIColor.myAppRedColor()
searchDisplayController?.searchBar.translucent = false
As you can see - that isn't exactly matching Navigation Bar color. It also has a separator between navbar and search bar.
Next solution almost worked:
searchDisplayController?.searchBar.backgroundImage = UIImage()
searchDisplayController?.searchBar.backgroundColor = UIColor.myAppRedColor()
But when I tap on the search bar it changes color:
It also has very weird animation:
https://youtu.be/Dq1GKBGbmTc
Do you have an idea what else I can do instead of implementing my own SearchBar?
Do not use Search Display Controller. Instead use only searchBar and TableView.
Simply delete Search Display Controller from your storyboard.
You will receive delegate methods of searchBar.
Here you can apply search on tableView.
I am facing a weird scenario, I have used a search bar in my application and tied it up with a table view as is seen in the below image!
But when ever I try to search anything the search bar slides up and gets hidden by navigation bar, I have used search bar a number of times and never seen me thing like this below is its screen shot !
I can't remove the navigation bar since I need it, but I just can't figure out why or how it can be happening,
One point to note is that the search bar and it's controller are working perfectly when tried to search, only thing is it's getting hidden behind the navigation bar,
Any help on this would be appreciated, as I just can't figure out its reason.
Try this:
Set the navigation bar's translucent property to NO:
self.navigationController.navigationBar.translucent = NO;
This line will fix the view from being framed underneath the navigation bar and status bar.
If you have to show and hide the navigation bar, then use this code in viewDidLoad
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone; // iOS 7 specific
Hey I have been trying to customize a UISearch bar with display controller and have run into some issues customizing certain aspects of the search and display controllers. I'm using storyboard and am developing with the most recent version of IOS 6.
The first issue is eliminating the dark grey gradient overlay that appears when you first click on the search text view. I have tried to use the searchDisplayController:willShowSearchResultsTableView: delegate but am unable to either find the view that is being used as the gradient overlay or a property to disable it. How do I remove this view?
The second is I am having an issue with the initial animation that is triggered when the search view is clicked. The animation is supposed to expand the search bar over the UINavigationBar to allow for a larger space to view the results. This doesn't happen and the search bar stays below the UINavigationBar and the animation doesn't properly show. I have tried setting the displaysSearchBarInNavigationBar and navigationItem properties but couldn't find any way to access or set them. Could this be an issue with the fact that I'm using autolayout in my story board? I have noticed that many issues relating to the UISearchBar animations are due to autolayout being used in the .xib or storyboard file. How can I make it correctly over the navigation bar?
The third issue I am having is with over ridding the searchBarTextDidBeginEditing:(UISearchBar *)searchBar delegate method. I have subclassed the UISearchBar to allow for complete customization. Is there a way to provide a default definition for the searchBarTextDidBeginEditing: delegate method inside my custom UISearchBar subclass. I want to provide a specific style of text in the UITextView when the user clicks on the search bar and don't want to have to redefine this method in every view controller that uses the search bar. Is this possible?
Thanks for the help in solving these issues.
I have a tableview with a search bar. All is working fine but now I want a button to hide/show the search bar. This functionality is what my question is about.
Now I have created a bar below the screen where I have created a button for this. In code, there is a void function which can be filled with the code I am looking for. Just to give you an impression of how the code looks like. Right now it is empty.
I've done some research and I came up with bringSubViewToFront and sendSubViewToBack, HeadsUpUI sample app. The idea I had was that I could show the search bar by bringing it to front, put OVER the table view layer, while setting the tableview 44 pixels lower with content offset or so.
Likewise, sending the search bar layer to back, BEHIND the table view to hide it. So in the void function I tried something with [parentview sendSubViewToBack:child view] but to no effect. So my question is: how do I hide or show my search bar while having a table view and a navigation bar and a bar below with the button to do so?
Kind Regards,
John
Use like -
childview.hidden = YES;
EDIT 1-
childview.hidden = YES;
[parentview sendSubViewToBack:childview];
[tableView reloadData];
Hope it work for you.
You can use IBOutlate to your searchBar and then use this outlate to show or hide the search bar
Also don't forget to connect it with your searchBar in xib
two little questions regarding the UISearchBar/SearchDisplayController:
Is it possible to prevent/hide the overlay which appears when a searchbar is clicked?
If the Searchbar is entered, the Tabbar and the NavigationBar of the current Screen disappears (and dont reappear after leaving the SearchBar) - how can i prevent that?
Is it possible to prevent/hide the overlay which appears when a
searchbar is clicked?
Why don't you just use a UISearchBar with the current table and not a UISearchDisplayController? One of the main features of the UISearchDisplayController is it's modal type display. With only the UISearchBar, you can search the current view.