iOS 8 UISearchController's searchBar overlapping tableVIew - ios

I've making practises to use iOS 8 new features - UISearchController to display my tableView and result. But something strange happened. It seems like the searchBar is transparent.
Yes, the searchBar is overlapping with the tableView. I've search a lot in SO, but no help.
My implementation in viewDidLoad
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_mySearchController.searchResultsUpdater = self;
_mySearchController.dimsBackgroundDuringPresentation = NO;
_mySearchController.hidesBottomBarWhenPushed = YES;
_mySearchController.hidesNavigationBarDuringPresentation = YES;
_mySearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[_mySearchController.searchBar sizeToFit];
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
Did I miss something important?

It's because you've set _mySearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal. According to Apple's documentation:
UISearchBarStyleMinimal - The search bar has no background, and the search field is translucent.
Try deleting that line of code or setting it to UISearchBarStyleDefault instead.

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.

How to separate the searchBar and tableview in iOS8

I build the searchController and the tableview.
But I refer more web documents. There are always add the searchBar to tableview header.
like below:
self.displayTableView.tableHeaderView = self.searchController.searchBar;
But Now I don't want to add the searchBar to the tableHeaderView.
I try to add direct the view(searchBarBgVW ). But the searchrBar was not show at screen.
as below:
- (void)viewDidLoad {
[super viewDidLoad];
......
.........
// --- search bar declare ---
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
// self.displayTableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchBarBgVW addSubview: self.searchController.searchBar];
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
.....
....
..
}
If I open the comment about the tableheader like below:
self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
self.searchController.searchResultsUpdater = self;
self.displayTableView.tableHeaderView = self.searchController.searchBar;
[self.view addSubview: self.searchController.searchBar];
The search bar will show at the place (0,0),
But the TableHeader will show white block.
How can I add the searchbar place using the searchController at correct method?Or How can I remove the tableviewheader white block?
Thank you very much.
Apple has a (newish) UICatalog sample that includes all kinds of search capabilities. It is divided into pretty simple controllers.. It's provided in both Swift and ObjC.

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

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