UISearchController - Results show under searchbar - ios

i've been converting my searches from the old UISearchDisplayController to the new UISearchController. I've got it all working now but my last issue is with the results tableview that is shown.
The results tableview is displayed underneath the searchbar, and when you scroll down the tableview then goes through the status bar.
Attached are some images showing the issues, Does anyone have any suggestions as to why this is happening. I've included some of the set up code from my ViewDidLoad.
Search Tableview before searching
Search Tableview after searching & scrolling
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = YES;
self.navigationController.navigationBar.translucent = NO;
[self setNeedsStatusBarAppearanceUpdate];
// create a filtered list that will contain products for the search results table.
self.filteredItems = [NSMutableArray arrayWithCapacity:[self.listContent count]];
// Initially display the full list. This variable will toggle between the full and the filtered lists.
self.displayedItems = self.listContent;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.placeholder = #"";
// Add the UISearchBar to the top header of the table view
self.TV.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
UIView *theView = [[UIView alloc] initWithFrame:TV.frame];
[theView setBackgroundColor:[UIColor colorWithRed:0.090 green:0.388 blue:0.643 alpha:1]];
[TV setBackgroundView:theView];
[TV reloadData];
self.navigationItem.title = #"Search";
`
Thanks,
Andrew

Related

SearchController on Navigation Bar is invisible on iOS11

Until iOS 11 was released, I had a search bar added to a UITableViewController. It is hidden under a navigation bar and is visible when user scrolls the table up. On iOS11, adjusting contentOffset or tableview.bound won't hide the search bar properly, sometimes it does work and sometimes it hides the first row, too. So, I've decided to move the search bar to the navigation bar. But, for some reason, I don't see it anywhere. Here's my code at viewDidLoad
YearTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (#available(iOS 11.0, *)) {
//iOS11 -> on navigation bar
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barStyle = UISearchBarStyleDefault;
self.searchController.searchBar.showsCancelButton = NO;
[self.searchController.searchBar sizeToFit];
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.navigationItem setSearchController:self.searchController];
[self.navigationController.navigationBar setPrefersLargeTitles:NO];
[self.navigationItem setLargeTitleDisplayMode:UINavigationItemLargeTitleDisplayModeNever];
self.definesPresentationContext = YES;
} else {
//iOS10 or previous -> on table view
UITableViewController *tableViewControllerForSearch = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
tableViewControllerForSearch.tableView.dataSource = self;
tableViewControllerForSearch.tableView.delegate = self;
tableViewControllerForSearch.tableView.estimatedRowHeight = 40;
tableViewControllerForSearch.tableView.rowHeight = UITableViewAutomaticDimension;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:tableViewControllerForSearch];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
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.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// No border between cells
tableViewControllerForSearch.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}
EDITED: 2017/10/20
As suggested, I've renamed and simplified the structure.
I have the following structure on storyboard.
UINavigationController: Initial view controller
YearPageViewController: Inside, it has YearTableViewController
On iOS10, it works fine. On iOS 11, I don't see the searchBar.
Thanks for your input.
What about adding
self.navigationItem.hidesSearchBarWhenScrolling = NO;
Obviously the search bar is hidden by default and only is to be seen when scrolling.
Hope will helpful to you On iOS11
if #available(iOS 11.0, *) {
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
I known why your view controller is stacks. set 'NO' flag in dimsBackgroundDuringPresentation
self.searchController.dimsBackgroundDuringPresentation = NO;

SearchBarController Search bar hiding tableview cells in case of multiple autolayout constraint

I have two views in my view controller first one is uitableview and the second one is bottom fixed UIView. I have autolayout constraint set up for both views, the table is at top and the second one is fixed at bottom, in the table view I am adding search bar. On the view load everything renders fine but as soon as the search bar is made active, the first two cells slip under the navigation bar. If i remove the Bottom view and bottom constraint everything works fine.
Here is the sample code im running
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;
self.tableView.tableFooterView = [UIView new];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.top.mas_equalTo(self.view);
make.bottom.equalTo(self.bottomToolBar.mas_top);
}];
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
// Configure the search bar with scope buttons and add it to the table view header
self.searchController.searchBar.scopeButtonTitles = #[#"Country",#"Capital"];
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];

Navigation bar is hiding when i open my search bar

I have a UISearchController embedded in my Navigation Bar and when i click it, the whole navigation bar goes off the screen until i press another area on the screen and then it comes back. Here is a link to the video. (video mightent have uploaded yet so give it some time, the link does work)
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
// Use the current view controller to update the search results.
self.searchController.searchResultsUpdater = self;
//Setting Style
self.searchController.searchBar.barStyle = UIBarStyleBlack;
self.searchController.searchBar.barTintColor = [UIColor colorWithRed:49.0/255.0 green:49.0/255.0 blue:61.0/255.0 alpha:1.0];
self.searchController.searchBar.backgroundImage = [UIImage imageNamed:#"BG"];
self.searchController.searchBar.placeholder = #"Search Artists, Songs, Albums etc.";
self.searchController.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;
self.searchController.searchBar.tintColor = self.view.window.tintColor;
[self.searchController.searchBar setTintColor:[UIColor colorWithRed:(226.0/255.0) green:(56.0/255.0) blue:(83.0/255.0) alpha:(1.0)]];
self.searchController.searchBar.delegate = self;
self.navigationItem.titleView = self.searchController.searchBar;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = searchController.searchBar.text;
NSLog(#"You searched for %#", searchString);
[searchResultsTableView reloadData];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[self.view addSubview:searchResultsTableView];
}
You need to add the following
self.searchController.hidesNavigationBarDuringPresentation = NO;
to stop UISearchController hiding the nav bar when activated (ref docs link).

Prevent UISearchBar from indenting right when tapped

In my viewDidLoad, I programmatically create a search bar. When the search field is tapped, the search bar indents to the right. Why is this happening and how do I prevent it from doing so? I am not using autolayout.
- (void)createSearchBar {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar sizeToFit];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
self.edgesForExtendedLayout = UIRectEdgeNone;
}
you can try using the positionAdjustmentForSearchBarIcon: or setPositionAdjustment:forSearchBarIcon:
message calls to the searchbar inside the delegate call:searchBarTextDidBeginEditing:

UISearchBar within UIViewController

I am trying to add a search bar programmatically to an UIView (not a UITableView). For the moment, I am just trying to set the delegates properly so that when I click the "enter" button, a message is printed:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"enter button clicked");
[searchBar resignFirstResponder];
}
I already have some code working for an UITableViewController that I am trying to adapt. It looks like this (I am skipping all the parts of the code that are not relevant to the present discussion):
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) UITableViewController *resultsTableController;
- (void)viewDidLoad {
[super viewDidLoad];
self.resultsTableController = [[SearchResultsController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
self.tableView.tableHeaderView = self.searchController.searchBar;
}
Now the only line that doesn't transpose to the UIViewController case is the last one
self.tableView.tableHeaderView = self.searchController.searchBar;
My UIViewController is defined with a xib which contains a UIView called myView and a UISearchBar called searchBar. So I tried doing
self.myView = self.searchController.searchBar;
or
self.searchBar = self.searchController.searchBar;
but in the first case the searchBardoesn't even show and in the second case the delegation to self seems lost (nothing happens when I click enter).
What am I doing wrong ?
This is how you would add a UISearchBar to a UIViewController programmatically:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, 40.0)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.searchBar.tintColor = kRunnerSearchBarTintColor;
if ([RunnerUtilities isIOS7orAbove]) {
self.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchBar setBarTintColor:[UIColor colorWithRed:185.0/255.0 green:195.0/255.0 blue:202.2/255.0 alpha:0]];
}
self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];
Well, this is no more different than adding it from XIB file. First, search bar is a added as a subview to view of view controller and should be connected to search bar outlet defined in ViewController. Second, UIViewController must be set as delegate of the search bar. Please ensure you are following this and this should work.
EDIT::
If you are trying to use UISearchController within UIViewController, please try with following code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any custom init from here...
// Create a UITableViewController to present search results since the actual view controller is not a subclass of UITableViewController in this case
UITableViewController *searchResultsController = [[UITableViewController alloc] init];
// Init UISearchController with the search results controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// Link the search controller
self.searchController.searchResultsUpdater = self;
// This is obviously needed because the search bar will be contained in the navigation bar
self.searchController.hidesNavigationBarDuringPresentation = NO;
// Required (?) to set place a search bar in a navigation bar
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
// This is where you set the search bar in the navigation bar, instead of using table view's header ...
self.navigationItem.titleView = self.searchController.searchBar;
// To ensure search results controller is presented in the current view controller
self.definesPresentationContext = YES;
// Setting delegates and other stuff
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
}

Resources