I have a UITableView with a search bar inserted programatically into the table's headerView:
override func viewDidLoad() {
super.viewDidLoad()
resultSearchController = UISearchController(searchResultsController: nil)
resultSearchController.searchResultsUpdater = self
resultSearchController.dimsBackgroundDuringPresentation = false
resultSearchController.searchBar.delegate = self
resultSearchController.searchBar.placeholder = "Search Contacts"
resultSearchController.searchBar.scopeButtonTitles = ["All", "Title1", "Title2", "Title3"]
tableView.tableHeaderView = resultSearchController.searchBar
resultSearchController.searchBar.sizeToFit()
definesPresentationContext = true
tableView.tableFooterView = UIView(frame: CGRectZero)
}`
The table is set up as a standard UINavigationController-based drill down - selecting a row pushes in a detail view controller.
Everything works perfectly in portrait or landscape, but breaks down after the detail view is popped if is has been rotated while displayed. The exact sequence is:
open the table in portrait, filter the results using the search bar, and select one of the results to push in the detail view
while in the detail view, rotate into landscape
while still in the detail view, rotate back into portrait
When the table view reappears, all the individual elements of the search bar have been "collapsed" on top of each other, and the whole thing overlays the status bar:
I've tried various ways of explicitly setting the frame of the search bar in viewDidAppear, as well as removing and replacing it from the header view - but the problem persists.
I've also noticed a similar problem with the Apple sample code from https://developer.apple.com/library/prerelease/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html :
Had anyone come up with a workaround for this??
Edited to add:
On further digging, it looks like the problem is that the navigation bar disappears after the rotation - here's before:
and after:
So, any ideas?
After noting that an inactive searchBar doesn't seem to have this problem, I have had success with saving the search bar state when the view disappears and restoring it before the view reappears.
The sample code works fine with the following changes. Save the state:
- (void)viewDidDisappear:(BOOL)animated
{
if (self.searchController.active && self.searchController.searchBar.text.length > 0) {
self.savedSearch = self.searchController.searchBar.text;
[self disableSearch];
} else if (self.searchController.active) {
// empty search field - this won't get restored
[self disableSearch];
}
[super viewDidDisappear:animated];
}
- (void)disableSearch
{
if (self.searchController.isActive) {
self.searchController.searchBar.text = #"";
self.searchController.active = NO;
}
}
And restore:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.savedSearch) {
NSLog(#"RESTORED SEARCH");
self.searchController.searchBar.text = self.savedSearch;
self.searchController.searchBar.showsCancelButton = YES;
self.searchController.active = YES;
self.savedSearch = nil;
}
}
This seems to work fine whether or not self.searchController.hidesNavigationBarDuringPresentation is set (although if true there is some animation on returning to the view).
Related
I have implemented a UISearchController in a TableView, pushed by a Navigation Controller.
First my problem was that whenever I click on the SearchBar, it disappears. It works when I enter some text, but it stays completely blank. Then I managed to semi solve the issue using this code:
- (void)searchForText:(NSString*)searchText
{
[self.view addSubview:villeSearchController.searchBar];
}
Which semi-works because now, when I click on the search bar, it blanks out, but if I enter one character, it appears again, and then it stays there, no matter what. Until I cancel the search, and click on it again, in that case it blanks out.
I have made some tests and this method (searchForText) is called on the very first click, so that isn't the reason.
Does anyone know how I can solve this issue and make the searchbar appear from the very first click?
EDIT:
This is how I initialize the SearchController:
villeSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
villeSearchController.searchResultsUpdater = self;
villeSearchController.dimsBackgroundDuringPresentation = NO;
villeSearchController.searchBar.delegate = self;
villeTableView.tableHeaderView = villeSearchController.searchBar;
villeSearchController.searchBar.scopeButtonTitles = #[];
self.definesPresentationContext = YES;
[villeSearchController.searchBar sizeToFit];
This happened to me when the UISearchController was hiding the navigation bar. Setting this property fixed it:
UISearchController.hidesNavigationBarDuringPresentation = NO;
Try to check the navigationBar.translucent property - it should be YES when UISearchController will present the searchBar or else will be UI bugs.
Update from #SiavA
The better solution is use the extendedLayoutIncludesOpaqueBars property of the UIViewController. If you using the opaque navigation bar just set it in the true for controller which will be show UISearchController (not for navigationController).
E.g.
- (void)viewDidLoad {
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
Place the SearchController inside a UIScrollView and it will work fine. This if you are using it in the section header or as a separate view
If you run into this problem in iOS11 (and especially if it worked pre iOS11), I had to change my UISearchController to be attached to the navigationItem rather than the tableView.
After setting parameters on my searchController, I used to do this:
tableView.tableHeaderView = searchController.searchBar
Now I have this:
navigationItem.searchController = searchController
The "translucent" fix would allow the controller to appear, but when I would try and unwind to a specific segue, I'd get a crash. Attaching the searchController to the navigationItem fixed both the display and the crash.
Setting isHidden of the navigation bar to false stopped the search bar from disappearing for me.
self.navigationController?.navigationBar.isHidden = false
Hi Guys there is a very simple solution for the issue.
-- this will solve major issues with collections of views and a parent view containing multiple viewcontrollers.
you don't need any below code if you have just remove it from your code
// searchController.definesPresentationContext = true
// self.definesPresentationContext = true
// self.extendedLayoutIncludesOpaqueBars = !(self.navigationController?.navigationBar.isTranslucent)!
I just add below code make sure you set searchController always set nil
if you are switching between viewControllers there may be some conflicts that can be cleared by setting it searchController nil in the setupsearchbar().
also, disable the searchController on viewDidDissapear(), it will solve the issue related to active search bar moving on to the next screen.
var searchController = UISearchController(searchResultsController: nil)
func setupSearchBar() {
searchController = UISearchController(searchResultsController: nil)
// adding search controller
searchController.searchResultsUpdater = self
// changing font color when user types
searchController.searchBar.searchTextField.textColor = .black
//allows select results from filtered table
searchController.searchBar.endEditing(true)
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
self.tableView.tableHeaderView = searchController.searchBar
}
override func viewDidDisappear(_ animated: Bool) {
searchController.isActive = false
}
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 want to have a searchBar in the tableView that is hidden by default, but if the user pulls down it appears.
So I have used the following code to implement this but when the view is first displayed the searchBar is momentarily visible and I would like avoid this brief flash. Adding self.tableView.contentOffset = CGPointMake(0, 0) to the viewDidLoad() method does not have any affect. Note that the tableView is behind a navigation controller which is why setting the Y offset to 0 effectively hides the searchBar behind the navigation bar. Any idea how to make sure the tableView's headerView is hidden when the view is first be displayed.
I have considered simply removing the headerView but then the user can't drag down to access it.
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44)
if let tv = self.tableView {
if let headerView = tv.tableHeaderView {
headerView.addSubview(searchBar)
} else {
FLOG("No table header view is available so create one!")
let headerView = UIView()
let width: CGFloat = tv.frame.size.width
headerView.frame = CGRectMake(0, 0, width, 44);
tv.tableHeaderView = headerView
tv.tableHeaderView!.addSubview(searchBar)
}
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
dispatch_after(0, dispatch_get_main_queue()) {
self.tableView.contentOffset = CGPointMake(0, 0)
}
}
I think there's an ordering issue with viewWillAppear and viewDidLoad. As I understand, viewWillAppear gets called every time a view will be drawn and added to the view hierarchy, whereas viewDidLoad gets called when a VC loads its subview for the first time.
So, I suppose on your first run the view loads via the mainQueue with viewDidLoad which places your search bar at (0,44), then viewWillAppear gets called but actually causes a brief visual blip due to the call to move the table's offset on the mainQueue. But on subsequent calls, since viewDidLoad has already happened, only viewWillAppear gets called. And since the search bar doesn't first start at (0,44) and then needs to move to (0,0), there's no blip.
Rather than directly defining the frame for the search bar, try calling sizeToFit on it after adding it to the tableHeader. Secondly, I don't believe you have to instantiate the header view, just set it equal to the searchbar. Lastly, take the contentOffset call out of the dispatch_queue, it should be handled correctly by viewWillAppear as is.
I went deep into UISearchController for iOS8 recently, and documented the journey (in part at least). For the code I used, here's a link to the implementation file and the corresponding write-up I did. It's in ObjC, so you'll need to do a little translation. But it looks like:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.filteredResultsTableViewController];
self.searchBar = self.searchController.searchBar;
// other code ...
[self.searchBar sizeToFit];
// other code ...
self.tableView.tableHeaderView = self.searchBar;
Using a universal storyboard with an adaptive UISplitViewController user interface.
I want to present a search controller on the primary (master) side, using the following code (from the master view controller):
static NSString * const kCGISearchViewControllerID = #"SearchViewControllerID";
- (IBAction)searchButtonClicked:(UIBarButtonItem *)__unused sender {
SearchViewController *searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:kCGISearchViewControllerID];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = searchResultsController;
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self presentViewController:self.searchController animated:YES completion:nil];
}
It initially appears to work correctly (regardless of the starting orientation):
Problems show up after autorotation (keyboard still visible, underlying content still dimmed, but the search bar has disappeared):
Upon rotating back to landscape, the search bar reappears, but its width is now wrong:
(I've also tried placing the search controller's searchBar in the titleView. While the searchBar adapts correctly, the search results controller still doesn't look right.)
What am I missing to get a presented UISearchController to animate itself properly as the UI adapts to changing size classes?
Update:
Adding self.definesPresentationContext = YES; gets the search bar/results to appear within the primary view, but the search bar is animating under that navigation bar, and isn't visible. The other issue is that the search bar height doesn't shrink, when it rotates from portrait (which has a status bar), back to landscape.
What Xcode version are you using? What iOS version on the simulator?
Tried that using Xcode 6, iOS 8.4 - That's all the code I used in the MasterVC:
class MasterViewController: UITableViewController {
#IBAction func search(sender: UIBarButtonItem) {
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
presentViewController(searchController, animated: true, completion: nil)
}
...
}
It's presented within the Master and locks the orientation of the screen! The behavior might have changed since you posted your answer.
I am building an iOS 7-only app. I am trying to set a UISearchDisplayController into the navigation bar.
I have it set up like this: In the storyboard, I added a "Search Bar and Search Display Controller" to my view controller's view, and set it at (0,0) relative to the top layout guide. I set constraints to pin to left, top and right. (I played with the constraints, i removed them completely, it doesn't matter) On top of that I have my Table view. When I added the search bar to the view in the storyboard, it automatically setup outlets for searchDisplayController and searchBar delegate. In code I have self.searchDisplayController.displaysSearchBarInNavigationBar = YES; I have two problems:
1) Without any buttons showing for the search bar (Interface builder -> select search bar -> Options: none selected) the search bar is in the middle of the screen:
If I click on the navigation bar, it starts editing the search bar:
notice also that the dark overlay appears to be offset from the navigation bar. It seems to me that the space is the same height as the navigation bar. Like it has been shifted down by that much. Also, when it displays the search results, the top of the content view is shifted down by the same amount (more pictures follow), which brings me to the second problem.
2) I messed around with it for a while and decided to check the option to have it show the cancel button. Now I have the search bar embedded in the nav bar correctly, but the overlay is still shifted down:
Again, when the search results table view appears, it is shifted down by the same amount (notice the scroll bar on the right side):
Even more bizarrely, I set a border on the search display controller's tableview layer, and it appears correct:
I have never used the UISearchDisplayController before and I unfamiliar with how to set it up, but functionally it works fine. I have read some other similar posts but the only advice is to hack it up by adjusting frames and setting manual offsets. I'd prefer to know what is causing this, is it a bug? Something I'm doing wrong? If it's a bug I can wait for a fix. It seems like such a basic thing that a thousand people must have done without any problem so I feel like I'm not setting it up correctly somehow. Thanks for you input.
I remember running into the same exact problem that you are observing.There could be a couple of solutions you can try.
If you are using storyboards
You should click on the view controller or TableView Controller which you have set up for your tableview and go to its attribute inspector and look under ViewController section and set the Extend Edges section to be under Top Bars.
If you are not using storyboards you can manually set the settings using the viewcontrollers edgesForExtendedLayout property and that should do the trick. I was using storyboards.
In my case, using storyboards, I had to check both Under Top Bars and Under Opaque Bars and leave Under Bottom Bars unchecked.
In my case, I actually had to uncheck all the Extended Edges boxes (essentially the same as programmatically setting Extended Edges to UIRectEdgeNone I believe) in my Storyboard in order to stop my search bar from offsetting itself. Thank you guys!
definesPresentationContext = true
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
self.tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
or see UISearchBar presented by UISearchController in table header view animates too far when active
My problem was just Adjust scroll view inserts. After change to false I didn't have problem
I had a same problem. And I solve this issue with adding view object under the tableview.
Add new ViewController on the Storyboard
Drag TableView to the new VC
Drag Table Cell to the TableView
Make a Connection for TableView DataSource, TableView Delegate to the new VC
I had very similar behavior happening. For me, the solution was to uncheck Extend Edges Under Top Bar in the storyboard settings for the parent view controller (I've turned off transparent navbars, not sure if that effects anything). If you're not using storyboard, you have to set [UIViewController edgesForExtendedLayout].
From the Apple docs:
This property is only applied to view controllers that are embedded in containers, such as UINavigationController or UITabBarController. View controllers set as the root view controller do not react to this property. Default value is UIRectEdgeAll.
Unfortunately none of the above solutions worked for me, I'm using a UITableViewController.
This link helped:
http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
I put the code below for convenience:
static UIView *PSPDFViewWithSuffix(UIView *view, NSString *classNameSuffix) {
if (!view || classNameSuffix.length == 0) return nil;
UIView *theView = nil;
for (__unsafe_unretained UIView *subview in view.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:classNameSuffix]) {
return subview;
}else {
if ((theView = PSPDFViewWithSuffix(subview, classNameSuffix))) break;
}
}
return theView;
}
- (void)correctSearchDisplayFrames {
// Update search bar frame.
CGRect superviewFrame = self.searchDisplayController.searchBar.superview.frame;
superviewFrame.origin.y = 0.f;
self.searchDisplayController.searchBar.superview.frame = superviewFrame;
// Strech dimming view.
UIView *dimmingView = PSPDFViewWithSuffix(self.view, #"DimmingView");
if (dimmingView) {
CGRect dimmingFrame = dimmingView.superview.frame;
dimmingFrame.origin.y = self.searchDisplayController.searchBar.frame.size.height;
dimmingFrame.size.height = self.view.frame.size.height - dimmingFrame.origin.y;
dimmingView.superview.frame = dimmingFrame;
}
}
- (void)setAllViewsExceptSearchHidden:(BOOL)hidden animated:(BOOL)animated {
[UIView animateWithDuration:animated ? 0.25f : 0.f animations:^{
for (UIView *view in self.tableView.subviews) {
if (view != self.searchDisplayController.searchResultsTableView &&
view != self.searchDisplayController.searchBar) {
view.alpha = hidden ? 0.f : 1.f;
}
}
}];
}
// This fixes UISearchBarController on iOS 7. rdar://14800556
- (void)correctFramesForSearchDisplayControllerBeginSearch:(BOOL)beginSearch {
if (PSPDFIsUIKitFlatMode()) {
[self.navigationController setNavigationBarHidden:beginSearch animated:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self correctSearchDisplayFrames];
});
[self setAllViewsExceptSearchHidden:beginSearch animated:YES];
[UIView animateWithDuration:0.25f animations:^{
self.searchDisplayController.searchResultsTableView.alpha = beginSearch ? 1.f : 0.f;
}];
}
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
[self correctFramesForSearchDisplayControllerBeginSearch:YES];
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[self correctSearchDisplayFrames];
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
[self correctFramesForSearchDisplayControllerBeginSearch:NO];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
// HACK: iOS 7 requires a cruel workaround to show the search table view.
if (PSPDFIsUIKitFlatMode()) {
controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(self.searchDisplayController.searchBar.frame.size.height, 0.f, 0.f, 0.f);
}
}
Go to storyboard.
Click on the view controller.
Go to attribute inspector under the ViewController section.
Set the Extend Edges section to be Under Top Bars and Under Opaque Bars.
Make sure to un-check Under Bottom Bars.