UISearchController without use of Navigation Controller - iphone

Is it possible to use the UISearchController without the SearchBar being displayed in the Navigation Controller? I basically want the SearchBar to stay on the screen and display the TableView beneath it:
What I was trying to do is this:
I've created a UITableViewController on the storyboard and linked it to the custom NPTableViewController class. Then I did this:
let resultsTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearch") as! NPTableViewController
searchController = UISearchController(searchResultsController: resultsTable)
searchController?.searchResultsUpdater = resultsTable
let searchBar = searchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
searchBar.frame.origin.y = 100.0
self.view.addSubview(searchBar)
Now when I run it, the searchBar is displayed but when I click on it the background dims, the searchBar disappears my resultsTable is also not displayed.

The solution I came up with is actually very simple. Instead of using UISearchController, I used a normal UISeachBar and UITableView. Since UISearchController doesn't do so much to help you, the work is pretty much the same if I kind of build my own SearchController.

Related

CollectionViewController does not show searchbar and navigation bar

I'm trying to follow along with a video course to implement photo gallery with Unsplash.
First I implemented UIViewController with searchbar and navigation bar in it
PhotoCollectionViewController: UIViewController
and manually adding searchbar and navigation bar
//MARK: - Nav bar inelkaar steken hier
private func setupNavigationBar(){
let titleLabel = UILabel()
titleLabel.text = "Photos"
titleLabel.font = UIFont.systemFont(ofSize: 15)
titleLabel.textColor = .black
navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: titleLabel)
navigationItem.rightBarButtonItems = [addBarButtonItem, actionBarButtonItem]
}
and the same for searchbar
so I got the result as this:
after this the PhotoCollectionViewController: UIViewController inheritance seemed be wrong what I did, and I need to change it to UICollectionViewController
I make then another controller inherited from UICollectionViewController with the same logic PhotoCollectionViewController: UICollectionViewController
It compiles without any issues, but I don't see navigation an search bar anymore
How can I fix this?
May be your collectionView is hiding search bar and navigation bar. check collectionView constraints. (specially top anchor)
// PS: I could comment, but I don't have 50 reputation.
maybe you can try this:
Check the top constraint first of your collectionView. And maybe you can add this code to you viewDidLoad() :
self.navigationController?.setNavigationBarHidden(true, animated: false)
if you use ScollView at fullscreen (SuperView), you can use this :
scrollView.contentInsetAdjustmentBehavior = .never
for searchBar IBOutlet you can add this :
searchBar.becomeFirstResponder()

How to remove strange highlighting of UINavBar when scrolling UICollectionView?

I have a child navigation view controller with a collectionView embedded in it. When I scroll it, the navigation bar appears to be dimmed a bit.
It looks like this:
Does anyone know how to remove this behavior?
I was setting my navigation bar appearance globally like that:
let appearance = UINavigationBarAppearance()
appearance.shadowImage = UIImage()
...
UINavigationBar.appearance().standardAppearance = appearance
But I finally managed to find the fix. You just have to add this:
appearance.backgroundEffect = nil
and the problem goes away.

UISearchController is visible at view start in a CollectionView with a custom layout

Edit: I changed the custom layout to flow, no changes. So I think the problem is not the custom layout
In my view controller with an embedded navigation controller, I have a CollectionView with a custom layout. In this view controller, I have implemented a UISearchController:
// Add searchbar in navigation controller
let searchController = UISearchController(searchResultsController: nil)
searchController.extendedLayoutIncludesOpaqueBars = true
searchController.searchResultsUpdater = self
searchController.automaticallyShowsCancelButton = false
searchController.searchBar.placeholder = ""
navigationItem.searchController = searchController
This code works fine for ViewControllers with tables etc, but when I open my view with the CollectionView, the search bar is visible at the start of the view, when it should be only visible by scrolling down.
My constraints for top is at the superview.
I have tried to look at the custom layout code to identify the problem, but was not able to find it.
How can I hide the search bar at view start?

tvOS: Is there anyway to prevent the search keyboard from collapsing on scroll?

So I have a standard setup for setting up a search view in my tvOS app.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let resultsController = storyboard.instantiateViewControllerWithIdentifier(SearchResultsViewController.storyboardIdentifier) as! SearchResultsViewController
let searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. iceland)", comment: "")
let searchContainer = CustomUISearchContainerViewController(searchController: searchController)
And what this is when pushed onto the navigation stack is a view that has the search bar on top and the results below like this
So whenever the results are large and I have to scroll the keyboard collapses and expands again whenever I scroll back up and swipe past the top most results. Is there any way to prevent this behavior? I just want it to stay open all the time. I was thinking of maybe subclassing UISearchController, but I wasn't sure what in there to even replace
I'm not sure this is the correct answer but if you do this your search bar will not collapse. I had set up a UICollectionView with a Horizontal scroll and I never had this issue. It may require laying out your view differently, and if you have hundreds of results this may not be the best solution, but it will keep the search bar and its 'keyboard' from going away.

Swift - bar button item image / set size and width

Feel like I've been wasting a lot of time on this one and would love some help. I'm trying to use a custom menu icon (20px x 20px) in my barButtonItem. The problem it that is scales to fill the bar button item and is distorted.
Normally this shouldn't be too complicated. But I'm using tabbed view controllers and not a navigation controller. The navigation bar I have dropped into the view controller is just an outlet.
Normally I could do something like this:
let image = UIImage(named: "menu_white")
let frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
let button = UIButton()
button.frame = frame
button.setImage(image, forState: .Normal)
let rightMenuButton = UIBarButtonItem(customView: button)
self.navigationItem.setRightBarButtonItem(rightMenuButton, animated: true)
But this doesn't work because self.navigationItem doesn't actually refer to anything in the view controller.
I need to somehow get the image into the outlet programmatically but setRightBarButtonItem is not a method of UINavigationBar.
#IBOutlet weak var navBar: UINavigationBar!
Would really appreciate some help if anyone's got some ideas.
The way a "loose" navigation bar works is that you configure a UINavigationItem and set an array containing that to be the navigation bar's items, as in this example code (from my book):
let ni = UINavigationItem(title: "Tinker")
let b = UIBarButtonItem(title: "Evers", style: .Plain, target: self, action: "pushNext:")
ni.rightBarButtonItem = b
self.navbar.items = [ni]
So, you could presumably do something like that.
The self.navigationItem thing is merely a way of causing that to be done automatically in the special situation where you're using a UINavigationController. But you say that you're not doing that. (Of course, you could do that; there's no law that says you have to use a navigation controller to do any actual navigation. I often use a navigation controller just to get the navigation bar, because it's a good place to put things like little menu buttons.)