Unable to stop animation and hide cancel button on searchBar of searchController - ios

I set my searchBar in the navigationBar. When activated it animates and the cancel button appears. I'm trying to stop animation and prevent cancel button from appearing. My story board is a tableviewController set in a navigationController.
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
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.navigationItem.titleView = self.searchController.searchBar;
[self.searchController.searchBar setShowsCancelButton:NO animated:NO];

Try [self.searchController.searchBar showsCancelButton:NO].

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;

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:

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?

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