UISearchDisplayController gap between overlay and bar - ios

In my app I'm trying to add search functionality. I have table view controller and custom top bar view where I show UISearchBar. The problem is that overlay view is always a bit under the top bar and it adds gap between them:
In my table view controller .m file's viewDidLoad:
[super viewDidLoad];
LSDropdownViewController *menuCtrl = (LSDropdownViewController *)[self parentViewController];
menuCtrl.topSearchBar.delegate = self;
[menuCtrl.topSearchBar setBackgroundColor:[UIColor clearColor]];
[menuCtrl.topSearchBar setBackgroundImage:[UIImage imageWithGradientColors]];
[menuCtrl.topSearchBar setTintColor:[UIColor whiteColor]];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:menuCtrl.topSearchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
any ideas?

It's due to iOS7+. So, to get rid of it, you have to set search bar frame manually by offsetting y-value 20 pixels i.e. searchBar.frame = CGRectMake(0,20,width,height).

Related

Scope buttons hiding behind the UITableview

I am creating a search screen using UISearchController, in which I have to show three scope buttons.I have put search bar programmatically.But somehow the scope buttons are hiding behind the UITableView.
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tblFoundList.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setDefaultTextAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]}];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:#"Search by" attributes:#{NSForegroundColorAttributeName: [UIColor lightGrayColor]}]];
self.searchController.searchBar.tintColor = [UIColor purpleColor];
self.searchController.searchBar.barTintColor = [UIColor groupTableViewBackgroundColor];
self.searchController.searchBar.scopeButtonTitles = #[#"A",#"B", #"C"];
[self.searchController.searchBar sizeToFit];
}
Could anyone help in this? Thank you.
As I mentioned earlier I had created UISearchController programmatically, so the UITableview in the image is UISearchController's UITableview, that is why nothing was affecting it.
So I got help from
UISearchController Search Bar Position Drops 64 points
and
UISearchBar presented by UISearchController in table header view animates too far when active
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
if (bar == self.searchController.searchBar) {
return UIBarPositionTopAttached;
}
else { // Handle other cases
return UIBarPositionAny;
}
}
On adding following method the search bar got shifted up and the scope buttons were visible.
But thank you all for helping out.

set location of uisearchbar in uiviewcontroller

I'm trying to add a searchbar to my viewcontroller. There are many tutorials with tableviewcontrollers, but not viewcontrollers. My problem is (like multiple other questions here) when selecting the searchbar the navbar moves up and searchtableview overlaps, but searchbar does not move up.
If I try to add it programatically I can implement it to the table header with:
self.tableView.tableHeaderView = mySearchBar;
In my UI there are buttons between the searchbar and tableview. I'd like the searchBar to be set right below the navigationBar. How would I do this?
UIView *searchBar_con=[[UIView alloc]initWithFrame:your_frame];
searchBar_con.backgroundColor=[UIColor clearColor];
UISearchBar *srchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 240 , searchBar_con.frame.size.height)];
srchBar.delegate = self;
[searchBar_con addSubview:srchBar];
UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:srchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
[self.view addSubview:searchBar_con];

UISearchDisplayController shift searchBar above frame in iOS6,but ok in iOS7

I embeded UISearchBar in navigationBar in my viewcontroller. Here is the code .I load SearchBar and UISearchDisplayController programmatically.
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
searchBar.tintColor = [UIColor darkGrayColor];
searchBar.showsSearchResultsButton = NO;
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
self.searchController.delegate = self;
searchBar.delegate = self;
[self.navigationItem setTitleView:searchBar];
}
And in viewWillAppear ,I add background image to my navigationBar.That's it. Not a complex view. It is working fine in iOS7. When user tap on SearchBar,it changes to editting mode searchBar is in the same position (x,y) . But when I tested on iOS6 , when tap on searchBar , we enters the editting mode but searchBar shifting up to above the screen. It is a behavior of searchDisplayController that try to cover view.
How can I solve this ? I tried to search through SO but has no luck so far.
Thanks in advance

UISearchBar Refuses to Change Width?

I am adding a UISearchBar to a UINavigation Bar as so (this is a storyboard based app) :
//Setup the search bar.
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 44.0f)];
self.tabBarController.navigationItem.titleView = searchBar;
searchBar.delegate = self;
controller = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
controller.searchResultsDataSource = self;
controller.searchResultsDelegate = self;
No matter what I set the width and height and position of the search bar to be, it will not change the height / width of the search bar. It is always the same width and height.
Can anyone suggest how to alter ?
I've experienced the same problem one time, and what I did was adding the searchBar into a container View (i've had problems showing the cancel button) and add the container view as a left bar item
- (UIBarButtonItem *)createSearchBarHolder {
UIView *searchBarContainer = [[UIView alloc] initWithFrame:navigationSearchBar.frame];
searchBarContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[searchBarContainer addSubview:navigationSearchBar];
navigationSearchBar.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *searchBarHolder = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
return searchBarHolder;
}
And in viewDidLoad:
NSArray *leftBarButtons = [[NSArray alloc] initWithObjects:[self createSearchBarHolder], nil];
self.navigationItem.leftBarButtonItems = leftBarButtons;
It's not clear what size you want your search bar to be. If you make it the navigation item's titleView, it will adjust for any buttons on the navigation bar, and adjust on rotation. Is that what you want?
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *sb = [[UISearchBar alloc] init];
self.navigationItem.titleView = sb;
}

UITableView Not Scrolling With UISearchBar

What I want to achieve is a UISearchBar that moves up and covers the UINavBar, and contains a cancel button on the right of it. All goes well, until I use the following line of code:
searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
What ends up happening is the UITableView just won't scroll, but everything else functions as expected. If I remove that line, my nav bar is visible, and the search bar just sits below it, also lacking a cancel button.
Any ideas?
The code for drawing the search bar is:
self.navigationItem.title = #"Search";
searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f)] autorelease];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchBar.keyboardType = UIKeyboardTypeAlphabet;
searchBar.delegate = self;
[searchBar becomeFirstResponder];
tableView.tableHeaderView = searchBar;
tableView.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f);
searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
searchDC.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
searchDC.searchResultsTableView.scrollEnabled = YES;
overlayView.frame = CGRectMake(0, 50, 320, 480);
[self.view addSubview:overlayView];
[self.view bringSubviewToFront:overlayView];
Not enough information to answer this. You need to show the UIViewController or UINavigation Controller code (both the .h and .m) where you are setting up the UISearchDisplayController.
EDIT:
You're implementing this totally wrongly. Apple has a great example on how to implement this http://developer.apple.com/iphone/library/samplecode/TableSearch/Introduction/Intro.html
From Apple's Documentation,
Delegate for the search display controller (delegate), which responds to events such the starting or ending of a search, and the showing or hiding of the search interface.
Set the delegate to your UITableViewController
searchDC.delegate = self;
Also add the searchDC's searchBar to the tableView's headerView
[self.tableView setTableHeaderView:searchDC.searchBar];

Resources