Navbar dissapears when view is displayed from segue - ios

I have a TableViewController that has an embedded NavigationController. There is also a ViewController to add new locations to the tableview.
This shows the relevant section of the Storyboard
When the user clicks a choose location there is a segue action through the NavigationController to the LocationChoiceTableViewController... a shortened version of the display is...
As you can see my navbar is showing with two buttons to add or edit the list. If a user clicks Add the segue action takes them to the AddLocationViewController...
The user adds details of the new location and clicks Add which has a segue action back to the LocationChoiceTableViewController passing back the values entered as a single concatenated string (newLocationtoPass)
The viewDidLoad in LocationChoiceTableViewController class has....
override func viewDidLoad() {
super.viewDidLoad()
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
if let savedLocations = defaults.objectForKey("locations") as? NSData {
locations = NSKeyedUnarchiver.unarchiveObjectWithData(savedLocations) as![String]
}
if newLocationtoPass != nil {
// we have a new location passed via segue from AddNewLocationViewController
//add new location to locations array and sort
insertSorted(&locations, newItem: newLocationtoPass)
// save location array to NSUserDefaults
saveLocationArray()
}
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationItem.leftBarButtonItem = self.editButtonItem()
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
}
Which if newLocationtoPass is not null will insert the value into the array (in the correct sorted position) and save the array to NSUserDefaults... All this works as shown in the screenshot below...
My problem is that I have lost my navbar with the edit/add buttons. I added the line self.navigationController?.setNavigationBarHidden(false, animated: true) but the navbar does not show when returning from AddLocationViewController.
Any help in fixing this would be appreciated.

try to write self.navigationController?.setNavigationBarHidden(false, animated: true) in viewWillAppear .. this may help you..

Fixed... I changed my Storyboard so that the segue actions from the Cancel and Add buttons on the AddLocationViewController went to the NavigationController and not the LocationChoiceTreeViewController.

Related

How to implement a SearchController with a SearchResultsController

Hello I am trying to implement a SearchController into my application however, my application has different tabs in which I would like them to see the search functionality. I have added a button to the NavBar and each of these tabs has a main controller view which includes this code. Unfortunately when I try to search for something the table view does not display with the results. Thank you for your help! Please find the code below:
MainController:
// set up search:
func setUpSearch(){
self.searchDataSource = DataTableViewController() // UIViewController, contains all functions for tableview plus outlet of tableview
self.searchController = UISearchController.init(searchResultsController: searchDataSource)
self.searchController?.searchBar.delegate = self
self.searchController?.searchBar.showsCancelButton = true
self.searchController?.searchBar.placeholder = "Search for people or places"
}
// user clicked on search button:
func searchButtonClicked(){
self.searchController?.isActive = true
self.searchController?.searchBar.becomeFirstResponder()
if let controller = self.searchController {
present(controller, animated: true, completion: nil)
}
}

searchController disappearing when clicking on search item

Hello I have a searchController which disappears on click on one of the search items that appeared before the new view displays. What I would like is for the search controller to stay active so when the user goes back to the view they still see the list of results. currently I display the search controller with a present function. Any ideas how to stop this from leaving the screen? Thank you in advance!!
MainViewController:
func searchButtonClicked(){
self.searchController?.isActive = true
self.searchController?.searchBar.becomeFirstResponder()
if let controller = self.searchController {
present(controller, animated: true, completion: nil)
}
}
func searchItemClicked(){
self.searchController?.isActive = false
//set up detail controller and push
let vc : detailViewController =self.storyboard?.instantiateViewController(withIdentifier:"DetailViewController")
self.navigationController?.pushViewController(vc, animated: true)
}
func setupSearch(){
self.searchController = UISearchController.init(searchResultsController: searchResultsCustomController)
self.searchController?.obscuresBackgroundDuringPresentation = true
self.searchController?.searchBar.delegate = self
self.searchController?.searchBar.showsCancelButton = true
self.searchController?.searchBar.placeholder = "Search for people or places"
}
EDIT:
To add more information regarding this error. The navigation bar has a search button which the user clicks to do a search. From here the searchController is started and a search bar appears. Upon entering credentials for the search the table view appears with results. If the user clicks on one of this results, the table view and the search bar disappear by going up as if I pressed the cancel button, and then the screen is pushed to the detail view controller.
For anyone running into this issue all you have to do is add the following line of code on your viewdidload function:
self.definesPresentationContext = true

Show search bar with action (bar item)

I have a problem with search bar. I need to create a table view with a search button in the right corner, and when I click on it, it should show the search bar.
My code is here:
// Search controller
searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.delegate = self
controller.searchBar.delegate = self
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = true
controller.searchBar.sizeToFit()
return controller
})()
And here is action:
// Search action
#IBAction func search(sender: UIBarButtonItem) {
print("Open search")
searchController.active = true
if searchController.searchBar.isFirstResponder() == false {
searchController.searchBar.becomeFirstResponder()
}
}
When I click on the button, nothing happens (only prints text in console), and what I want is in the image below:
Your class needs to conform to a UISearchBarDelegate, I'm not sure if you've done that already. Also make sure you present the search view
From Apple's Docs:
The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the text field.
Here's a sample from my app
#IBAction func searchAction(sender: UIBarButtonItem) {
// Create the search controller and specify that it should present its results in this same view
searchController = UISearchController(searchResultsController: nil)
// Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)
}
Old question, but want to add a new answer (maybe things changed since the original answer from 2016).
Simply call this on the view controller, assuming you already set up the search controller. No need to conform to UISearchBarDelegate:
Swift 5
present(searchController, animated: true, completion: nil)

un-hiding tabbar ios swift

I'm working with two table view controllers. When a button is pressed on a cell from table view a, table view b comes up on top of a.
In the code for tableview a I wrote that when the button that takes you to table b is pressed self.tabbarcontroller?.tabbar.hidden = true.
That worked for hiding the tabbar but when i put the code in the second table view self.tabbarcontroller?.tabbar.hidden = false the tabbar doesnt come back.
// present view controller
let vieww = self.storyboard?.instantiateViewControllerWithIdentifier("viewTable") as! viewsVC
vieww.view.backgroundColor = .lightGrayColor()
vieww.view.alpha = 0.9
vieww.modalPresentationStyle = .OverCurrentContext
vieww.hidesBottomBarWhenPushed = true
self.presentViewController(vieww, animated: true, completion: nil)
// hide tabbar
self.tabBarController?.tabBar.hidden = true
Add the code
self.tabBarController?.tabBar.hidden = false
to the ViewDidAppear method, rather than the viewDidLoad method in the original tableViewController.
The issue you are encountering is that the view is not loaded after you go back, but rather appears right away since it has already been loaded beforehand.
It should look like this:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tabBarController?.tabBar.hidden = false
}
This should fix the problem.
Good luck with your project!

Swift UISeachController add nav bar on selection

I currently have my UISearchController set up so it is presented over the nav bar once the search icon is clicked.
var searchController: UISearchController!
#IBAction func searchMe(sender: UIBarButtonItem) {
// Create the search results view controller and use it for the `UISearchController`.
let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier(SearchResultsViewController.StoryboardConstants.identifier) as! SearchResultsViewController
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = Colors.blue
searchController.searchBar.barTintColor = UIColor.whiteColor()
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
I am using a different searchResultsController than the current table view to display results. The problem is that when a cell is selected in the searchResultsController a segue is performed to a new view controller (the users profile) and there is no navigation bar.
I need a back button to appear to take the user back to the searchResultsController. I have tried a bunch of different segues, setting the nav bars on the storyboard but nothing seems to work. My initial thought was to make this line of code:
searchController = UISearchController(searchResultsController: searchResultsController)
point to a nav controller with searchResultsController as its root view controller but I had no luck. Any help is great! Thank you!

Resources