I have been having this issue with the status bar and tab bar.
Here is my code:
self.searchResultsController = [BZDashboardSearchResultsController new];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
self.searchController.hidesBottomBarWhenPushed = YES;
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = YES;
self.definesPresentationContext = YES;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.view.backgroundColor = [UIColor whiteColor];
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
[self presentViewController:self.searchController
animated:YES
completion:nil];
I want to hide the tab bar place the search bar under the status bar. My view controller hierarchy looks like this:
UITabBarController
--UINavigationController
---View Controller where the above code is executed.
In my app delegate, I set my UITabBarController subclass as the root view controller:
self.window.rootViewController = tabBarViewController;
The init method of the subclass is this:
-(instancetype)init {
self = [super init];
if (self) {
UINavigationController *workspaceNavigationController = [[UINavigationController alloc] initWithRootViewController:self.workspaceController];
self.userProfileViewController.showLogoutButton = YES;
UINavigationController *userProfileNavigationController = [[UINavigationController alloc] initWithRootViewController:self.userProfileViewController];
self.viewControllers = #[workspaceNavigationController, userProfileNavigationController];
}
return self;
}
self.workspaceController has a search button in its navigation bar the code that presets UiSearchController.
Related
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;
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()
I'm building an iOS app and I'm creating a custom UIView which contains UIWebView some UIButton and I'm trying to add UISearchController the problem is that the view doesn't have UINavigationController so I created one and I tried to put my UISearchBar into titleView but it doesn't work .
Here is my code, any idea please !!
- (void)initView {
self.navigationController = [[UINavigationController alloc] init];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:#"bakground1"]init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor hx_colorWithHexRGBAString:#"#332532"];
[self initializeSearchController];
self.webView = [[MapWebView alloc] initWithFrame:CGRectMake(0, 100, widthtScreen, heightScreen)];
self.webView.mapWebViewDelegate = self;
[self addSubview:self.webView];
}
- (void)initializeSearchController {
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.navigationController.definesPresentationContext = YES;
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.accessibilityElementsHidden= true;
self.searchController.dimsBackgroundDuringPresentation = true;
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.navigationController.navigationItem.titleView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
}
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?
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;
}