UISearchController searchbar disappear after clicked - ios

I have UISearchController in my application:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[self.searchController setSearchResultsUpdater:self];
[self.searchController setDelegate:self];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"9.1")) {
self.searchController.obscuresBackgroundDuringPresentation = NO;
}
self.searchController.searchBar.showsCancelButton = YES;
[self.searchBarView addSubview:self.searchController.searchBar];
self.searchController.searchBar.delegate = self;
The issue is that after i click the search bar it disappears from the screen:
Any idea how i can fix this problem?

add these line of code in your controller
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
hope it will solve your issue ... if not ... let me know in comments

Related

UISearchController UISearchBar is not vissible in IOS 12

I have UISearchController in my app and this is the code I'm using to create it:
-(void)setSearchBar {
self.resultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.resultsController.tableView registerClass:UITableViewCell.self forCellReuseIdentifier:#"SearchTitleCell"];
self.resultsController.tableView.dataSource = self;
self.resultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsController];
[self.searchController setSearchResultsUpdater:self];
[self.searchController setDelegate:self];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"9.1")) {
self.searchController.obscuresBackgroundDuringPresentation = NO;
}
self.extendedLayoutIncludesOpaqueBars = YES;
self.navigationItem.searchController = self.searchController;
self.searchController.searchBar.delegate = self;
}
I noticed that in IOS 13 it's working fine, but in IOS 12 I can't see the searchbar as shown in the photos:
IOS 13:
IOS 12:
Any idea what is the problem?
In iOS 12 the search bar is shown below the results controller.
To fix that you have to set self.definesPresentationContext = true for the main controller in your code.
You can also do this on a storyboard.

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;

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:

UISearchController issues iOS8 vs iOS9

I ran into a problem where the UISearchController, when it animates the searchBar into the navigationBar, leaves the statusBar transparent and not the same color as the searchBar, like below:
I searched SO, and the below solution inspired by this question
- (void)willPresentSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = YES;
}
- (void)willDismissSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = NO;
}
fixes the problem for iOS9, but not for iOS8. I tried setting
self.extendedLayoutIncludesOpaqueBars = YES;
but with no effect. Also, setting
self.edgesForExtendedLayout = UIRectEdgeAll;
hides the initial searchBar under the navigationBar, so I cannot use it.
Here's my code for initiating a UISearchController under an opaque UINavigationBar:
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
self.searchController.searchBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0); // Required for iOS8, not iOS9 though
[self.view addSubview:self.searchController.searchBar];
Any suggestions how to fix this for iOS8?

Unable to push view controller when I use UISearchController

I have UISearchcontroller. When I select a searched item and try to push a viewcontroller, the viewcontroller is not being pushed. The reason for this issue is when I set self.definespresentationcontext to NO. Its working if I set the self.definespresentationcontext to YES, but the searchbar beomes hidden.
Example :
self.searchResultController = [[MyResultsController alloc]init];
self.searchResultController.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
self.searchController.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.showsCancelButton = YES;
self.searchController.searchBar.translucent = YES;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = NO;
Can somebody help me out to solve this issue?
Thanks in advance.
Maybe
presentingViewController?.navigationController?.pushViewController(eventDetailsViewController, animated: true)
Make sure your UIViewController is set to render under Top Bars and then everything should work as expected.
self.definesPresentationContext = YES

Resources