I have a UISplitViewController with a UINavigationController as the masterviewcontroller. This UINavigationController has a UICollectionViewController as its rootViewController. In the UICollectionViewController, I have set the following parameters:
self.collectionView.scrollEnabled = YES;
self.collectionView.bounces = YES;
self.collectionView.alwaysBounceVertical = YES;
self.navigationController.navigationBar.prefersLargeTitles = YES;
UISearchController *sc = [[UISearchController alloc] initWithSearchResultsController:nil];
self.navigationItem.searchController = sc;
The UISearchBar does show up in the navigationBar, but when I launch the app the navbar is collapsed to the small title. It's only when I drag the view downwards that the large title and the search bar appear.
Where I expect the view to launch like this
it actually launches like this
Any ideas what could be causing this?
Make sure you set preferLargeTitles to true in your UINavigationBar; either in code or in the Storyboard file.
Put this in your viewDidLoad.
self.navigationController?.navigationBar.prefersLargeTitles = true
Related
Through Storyboard, I have created a ViewController with a UITableView and a SegmentedControl on top. Programatially, I have added a SearchController as the TableView's header.
This is what happens when I launch the app:
It works perfectly.
But when I click on the search bar, it already messes up a bit. It's easy to fix by hiding the SegmentedControl though.
Now the problem appears when I finish my search:
The SegmentedControl disappears. Whenever I click on the searchbar again, it comes back, just like in the picture number 2, but if I cancel again, it disappears.
I have tried removing it from the view and adding it again once the user stops searching, but it changes nothing. Here is my code:
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = searchController.searchBar;
searchController.searchBar.scopeButtonTitles = #[];
self.definesPresentationContext = YES;
[searchController.searchBar sizeToFit];
What can I change?
searchController.searchBar.showsScopeBar = YES;
I have an UIViewController with a UICollectionView in it, and a UISearchBar on top of it. The problem is that when I tap on my search bar, and it hides the navigation bar, search bar background color is not extending under the status bar.
Here is how I set up the UISearchBar and UISearchController:
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:[ADC_SEARCH_RESULTS_CONTROLLER_IDENT copy]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44.0);
[self.view insertSubview:self.searchController.searchBar belowSubview:self.navigationController.navigationBar];
self.definesPresentationContext = YES;
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
SuggestionResultsTVC *vc = (SuggestionResultsTVC *)searchResultsController.topViewController;
vc.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.f, 0.f, 0.f);
self.searchController.searchBar.clipsToBounds = YES;
Here is a screen shot of the UIViewController when search bar is dismissed:
http://s4.postimg.org/x0rncg5e5/Screen_Shot_2015_09_02_at_5_09_41_PM.png
And here is a screen shot of the view controller when search bar is active:
http://s4.postimg.org/tv71m8mrx/Screen_Shot_2015_09_02_at_5_10_04_PM.png
In your storyboard or xib file, go to Attributes Inspector -> Simulated metrics and change status bar from "inferred" to "none".
I build the app in simulator, and activated the UISearchBar, then I enter the "View UI Hierarchy" option in xcode, here is a screen where you can find it:
http://s16.postimg.org/wyb8jxqw5/Screen_Shot_2015_09_02_at_5_47_58_PM.png
And there it was clear for me that the gray background color comes from a UIDimmingView under my UIViewController -> the solution was to change the color of the UIView of my UIViewController, that is above that UIDimmingView to the color matching UISearchBar background.
It looks to me like your UISearchBar may send a call to its parent's UINavigationController after being tapped. So try tinkering with this:
self.navigationController.view.backgroundColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = NO;
We have a UINavigationController with a UITableViewController. We have a UISearchController to filter the list of items in the table. When we tap a cell it navigates to the detail. But when we navigate back, its as if the UISearchBar is on top of the content... like instead of being in the table header, it is now over top of the table. How can we get it to behave as being in the table header.
func buildSearchBar() {
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
self.searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController.searchBar
}
Edit to add: we set all UINavigationBar appearance to be translucent = NO; This is the line of code that seems to break it for us. Does that sound right to anyone else?
[UINavigationBar appearance].translucent = NO;
I had a similar problem. Add the following line to the viewDidLoad method of your view controller (self in the code you posted in your question).
self.definesPresentationContext = true;
This fixed the problem for me.
I have added a UISearchController to my code using the following method:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = #[];
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;
This creates my search controller and adds it to the top of my tableView. Annoyingly it starts visible though:
I can hide it by sliding it up under the navigation bar which suggests the underlying functionality of the code is working but I can't get it to start hidden so I can slide it down.
I have tried adjusting the edge insets, I have tried setting the navigation bar to translucent, I have tried to go through the search bar tutorials online but nothing seems to be dealing with this issue.
Any help very welcome
Did you try setting the content offset of your table view?
[self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height) animated:NO];
Here is for swift 4
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: true)
From iOS 11.0 onwards you can use,
self.navigationItem.searchController = self.searchController;
The search bar will be hidden, unless you swipe down to reveal it.
How can I show a UISearchBar in the NavigationBar?
I can't figure out how to do this.
Your help is very much appreciated.
To put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.
Per the documentation:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.
I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.
1) Nib Based Approach
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
2) Code (Timing Matters)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar to the navigationBar first, then set your barButtonItem.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
Check out Apple's UICatalog sample code. It shows how to use the new UISearchController in three different ways: modally, in nav bar, and below the navigation bar.
Objective C code snippet for UISearchBar in NavigationBar
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (#available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here