0

I dragged a search bar and display controller to a view controller and placed it at the bottom of the outline.

I then set it to display in navigation bar with following line in viewdidload. All that works.

self.searchDisplayController.displaysSearchBarInNavigationBar = true; 

However, I want to hide it initially, and the following line in vewdidload is not having any effect.

self.searchDisplayController.searchBar.hidden = true; 

Can anyone suggest what to do?

Thanks in advance for any suggestions.

EDIT:

I think I have figured out that you cannot have searchBar.hidden = true and displaysSearchBarInNavigationBar at the same time. Which is fine by me as I only want one or the other. So I tried separating the two cases.

First in view did load, I put

self.searchDisplayController.searchBar.hidden = true; 

This does work if by itself.

Then I have a search button wired to an action method as follows:

 - (IBAction)searchButtonPressed:(id)sender { NSLog(@"search button pressed"); self.searchDisplayController.searchBar.hidden = false; self.searchDisplayController.displaysSearchBarInNavigationBar = true; self.searchDisplayController.searchBar.showsCancelButton = YES; } 

However, while I can see from the log that the method is called, it does not unhide the search bar. Perhaps it is delegate issue but still looking for solutions.

3
  • Do you only want to hide the searchBar? Then searchBar.hidden = trueis enough. Commented Jan 5, 2016 at 18:35
  • That gives me use of undeclared identifier Commented Jan 5, 2016 at 18:53
  • Check the answer, if you´re having trouble adding the outlet just post a comment to my post and I´ll help you. Commented Jan 5, 2016 at 18:57

2 Answers 2

1

Do it like this

  1. Add an outlet to your searchBar from your Storyboard
  2. Then you can use the outlet identifier to hide the searchBar by adding the following row

searchBar.hidden = true

Sign up to request clarification or add additional context in comments.

2 Comments

I created the outlet but it does not seem to toggle the search bar on and off i.e. true or false does not seem to have any effect. Perhaps I should create an outlet for the searchDisplayController and try to toggle it?
You could try that, but that should not be necessary.
0

=> try it.

self.definesPresentationContext = true

OR-------------

searchController.hidesNavigationBarDuringPresentation = false 

=> more If You Want to know given Refrence below might be useful:-

How to implement UISearchController in UITableView - SWIFT

UISearchController doesn't hide view when pushed

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.