Keep uisearchbar in place when user taps search - ios

I have a UISearchBar in a tableviewcontroller like so:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchBar;
When the user taps in the search bar to search, the searchbar expands to show the cancel button, but then also moves down about 20 pixels, so in effect there is a 20 pixel blank space above the searchbar between the search bar and the navigation bar. I don't understand the moving down behavior and what I can do to prevent it. Does anyone know what exactly might cause that to happen and how I can prevent it?

I was having the same issue, although with a UISearchBar being presented by UISearchController. For me, removing the line
self.edgesForExtendedLayout = UIRectEdgeNone;
from the presenting VC's viewDidLoad method stopped the search bar dropping down.

Related

Hide Searchbar without leaving blank space when using UISearchController in IOS

The default behavior for UISearchController when you create it in code with:
self.searchController = [[UISearchController alloc]
with iOS 13 seems to be rather than hiding the searchBar to show it at the top or above a tableview immediately below the navigation view.
Then when the searcher becomes firstResponder, the searchBar moves up inside the navigation view as follows:
providing you have set
self.navigationItem.searchController = self.searchController;
You can hide the searchBar by changing the hidden property to YES as follows:
self.searchController.searchBar.hidden = NO;
However, it leaves white space.
To counter this I have tried manually moving the Tableview and buttons upwards, however, that seems to have no effect.
How can I hide the searchBar prior to it becoming active without leaving a white space where it is hidden?
Thanks for any suggestions.

Two problems with UISearchBar when it is embedded in UINavigationBar

I set a default background image for UISearchBar:
[[UISearchBar appearance]setSearchFieldBackgroundImage:[UIImage imageNamed:#"search_field"] forState:UIControlStateNormal];
I then have a UISearchBar set to the nav bar's titleView.
UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 35)];
lsearchBar = [[TestSearchBar alloc]initWithFrame:searchView.frame];
[searchView setBackgroundColor:[UIColor clearColor]];
[searchView addSubview:lsearchBar];
But the result looks horrible with a crushed graphic for my search bar:
If it try to embed it without using UIView directly, like this:
lessonsSearchBar = [[TestSearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
self.navigationItem.titleView = lessonsSearchBar;
it looks better, but when I push segue to the next view controller, or do pop back with animation it tries to animate it and looks horrible with this gray bar around it:
I do try it inside UIView because I also want to set a custom size to my UISearchBar and realign it a bit to the left side of the screen, which for some reason is not allowed when I set my search bar directly to navitationItem.titleView. I'm not sure exactly how to achieve this without crashing my image (which I set with setSearchFieldBackgroundImage) or without those glitches with animation. Can you please help?

UISearchBar Overlays UITableView - TableView Can't be moved

I've got a TableView and a Search Bar set up and both work fine so far. The problem is that the UISearchBar overlays the first item of my TableView.
I cannot use XIB or storyboards in this project.
The definition and instantiation of the TableView is in a galaxy in a class (imported by a class I import...) far, far away -- not that easy to access.
I move (instantiate) the SearchBar on the screen with:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;
I tried moving it to 0,-44 but then the screen does not stay at the SearchBar, it moves back down.
If I try to move the UITableView with:
self.tableView.frame = CGRectMake(0, 0, 320, 500);
it moves the SearchBar as well.
So let's say if I set the origin of the search bar at 0,0 and the origin of the table view at 0,44 I get a screen with (objects in this order):
Grey Space with the height of a search bar/table view cell
Search bar (still overlaying the first cell of my table view
other cells
How can I solve this problem? I think the easiest way would be to add an empty object to my data array so the search bar overlays nothing of importance, but that would not be the nicest way of solving it.
Could anyone tell me how I can add/insert an object to an array and then copy another array into it, beginning with index 1, so my empty object stays in there?
You can use contentInset property on your tableView. Just set the top inset to height of search bar.
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height, 0, 0, 0);
This should solve the issue.

UISearchBar automatically resizes and changes frame

I have an issue with a search bar that behaves in a strange way when it becomes a firstResponder and when it resigns.
The search bar is added as the header of a table view
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource = self;
The view controller is set a left panel of JASidePanelController and it hides the center panel when the keyboard shows or hides :
- (void)keyboardWillAppear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:YES
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = YES;
}
- (void)keyboardWillDisappear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:NO
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = NO;
}
Normal state
When the search bar becomes a firstResponder it either moves about a point up or point down randomly
And When the search bar resigns it animates up to reach the window origin and then back to its natural frame
Here is a sample project reproducing the bug.
EDIT :
As per #kwylez suggestion, the unwanted animation that the search bar makes when it resigns can be avoided by:
self.searchBar.clipsToBounds = YES;
I solved this issue by creating a UIView with ClipBounds sets to YES and then add subview the searchbar inside it.
Then include it in tableview header. its working now.
Thanks
You initialize a search display controller with a search bar and a view controller responsible for managing the data to be searched. When the user starts a search, the search display controller superimposes the search interface over the original view controller’s view and shows the search results in its table view.
customized your searchbar view
Fixed - UISearchBar-bug-master
I traced the issue to the function "_layoutSidePanels" in the JASidePanelController.
In your app delegate, I commented out the following code and it seems to fix the grey view growing and shrinking.
rootViewController.shouldResizeLeftPanel = YES;
If you follow the code through, when the searchbar is selected you call setCenterPanelHidden, which subsequently calls _layoutSidePanels, which runs the following code:
if (self.leftPanel.isViewLoaded) {
CGRect frame = self.leftPanelContainer.bounds;
if (self.shouldResizeLeftPanel) {
frame.size.width = self.leftVisibleWidth;
}
self.leftPanel.view.frame = frame;
}
Changing the frame of the sidepanel seems to be the cause, and as I said commenting that code out fixes the issue on my end.
Edit: Also at first it seemed like the search bar was moving up and down a point, but upon further inspection it appears that it is always slightly underneath the navigation bar, but you don't notice it until you select the searchbar and the rest of the view "greys" out, so that little space that was white between the blue nav bar and light grey search bar becomes dark grey like the rest of the tableview below.
Edit #2: Took me a while, but I managed to figure out where the heck that grey mask was coming from. Your UISearchDisplayController is what is responsible for the greyish background that appears when the search bar becomes first responder, and when I removed the following two lines of code the issue you were seeing went away:
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
Doing this was just to demonstrate the cause of the issue, but removing those lines of code disable whatever functionality you were going to gain from using the search display controller. I don't know exactly what you're hoping to do, so I can't really give you any advice about how to proceed, but hopefully I've pointed you in the right direction as to the causes!

IOS searchBar - Getting rid of the white space below

I have a view which has two containers: Top_Container and Bottom_Container.
Each Container points to a VC with a TableView.
The Bottom_Container points to a TableView with a searchBar on top.
Whenever the searchBar gets activated in the TableView a white space appears below the searchBar between the searchBar and the greyed zone corresponding to the serachBarTableView (which superposes the TableView).
I have been trying with no success to get rid of this white space with no success.
Anybody has an idea how to customize:
- the white space which appears below the searchBar ?
- the greyed zone (searchBar TableView ?) on top of the TableView which appears whenever the searchBar gets active ?
Thank you.
Try with following code:
CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor clearColor];
[self.searchBar addSubview:lineView];
It is fix.
I had similar problem and in my case it was caused by opaque NavigationBar. When I set NavigationBar to translucent, then the underlaying UITableView is correctly aligned to the active UISearchBar. (note: I'm using UISearchDisplayController in my view controller)
In your case maybe you can move (and animate) the underlaying table view in UISearchDisplayDelegate's methods willBeginSearch and willEndSearch. If you are using UISearchBar only, then you need to subclass it and override becomeFirstResponder and resignFirstResponder methods and implement the "table view moving" code there.
I just fixed this problem in my own code. For me, the issue was caused by 2 lines of code.
self.viewController.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationBar.translucent = NO;
After removing both these lines, everything worked as expected.

Resources