Unable to push view controller when I use UISearchController - ios

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

Related

UISearchController searchbar disappear after clicked

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

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;

UISearchController's UISearchBar embedded in Navigation Bar: Does not become first responder

I am trying to embed a UISearchController's search bar in the titleView of my navigation controller's bar. Exactly this can be seen in the UICatalog sample code by Apple and works:
// Create the search results view controller and use it for the UISearchController.
AAPLSearchResultsViewController *searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:AAPLSearchResultsViewControllerStoryboardIdentifier];
// Create the search controller and make it perform the results updating.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = searchResultsController;
self.searchController.hidesNavigationBarDuringPresentation = NO;
/*
Configure the search controller's search bar. For more information on how to configure
search bars, see the "Search Bar" group under "Search".
*/
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchController.searchBar.placeholder = NSLocalizedString(#"Search", nil);
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
I try to do the same with this code:
self.searchSuggestionsController = [[SearchSuggestionsTableViewController alloc]initWithStyle:UITableViewStylePlain];
self.searchSuggestionsController.delegate = self;
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchSuggestionsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self.searchSuggestionsController;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.searchController setHidesNavigationBarDuringPresentation:NO];
self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchController.searchBar setPlaceholder:NSLocalizedString(#"Search", nil)];
[self.navigationItem setTitleView:self.searchController.searchBar];
if (self.searchQuery) {
[self.searchController.searchBar setText:_searchQuery.queryString];
}
self.definesPresentationContext = YES;
But when I run it on device or in simulator the search bar in the navigation bar will never become first responder. The keyboard is not showing up, I can't enter text. What I can do is use the clear button and the text clears but still I can't enter any texts.
Any ideas?
Old question, but just had a similar problem. Be sure to have embedded the "searchBar" in a valid navigation controller in the VC heirachy. That happened to be my mistake. So this works;
CCCTestVC *tVC = [[CCCTestVC alloc] init];
UINavigationController *nC = [[UINavigationController alloc] initWithRootViewController:tVC];
[self.navigationController presentViewController:nC animated:YES completion:nil];
as opposed to doing this;
CCCTestVC *tVC = [[CCCTestVC alloc] init];
[self.navigationController pushViewController:tVC animated:YES];
try implementing UISearchBarDelegate
and call searchBar' s becomeFirstResponder() in searchBarTextDidBeginEditing
then in searchBarTextDidEndEditing call resignFirstResponder()

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 in UITabBarController is not responding to touch

I have a tabbarcontroller, which is inside of a uinavigationcontroller, and I am trying to put a searchbar in the tableviewheader on one of the tabs. I am able to successfully use the code below on any screen on the navigationcontroller but the search will not respond to touch when the view is on the tabbarcontroller. I can successfully set it to active in code but I need touch to work as well.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = #[#"SEARCH"];
[self.searchController.searchBar setPlaceholder:#"SEARCH"];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.searchController.searchBar.backgroundColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:1];
self.searchController.searchBar.tintColor = [UIColor blackColor];
self.myTableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
I had a similar issue and found that the line self.definesPresentationContext = YES; was the root cause of the problem (for more information: UISearchController and definesPresentationContext).
Make sure that the class setting self.definesPresentationContext = YES; is also a parent view controller of the search bar. If you're initializing the UISearchController using the code you provided and then assigning the search bar to a view outside the hierarchy this might be causing the problem.

Resources