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

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

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.

searchBar should not be visible when showing view - ios 11

I have similar problem like here:
SearchBar in NavigationBar IOS 11
In my case I don't have toolbar on top of TableView.
I added searchBar in my VC:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationController.navigationBar.prefersLargeTitles = NO;
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
}
Now searchBar is always visible. It should be hide by default. SearchBar should showed when I pull the tableView down.
What am I doing wrong?
The problem is that I add picture to background:
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"somethingImage"]];
[background setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
Then Search controller has some problem. I don't know how to repair it. I think it is an apple bug.

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 gap between overlay and bar

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

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