Benefits of using UISearchController? - ios

What's the benefit of using UISearchController over UISearchBarDelegate? The difference seems kind of subtle to me. In particular, updateSearchResults seems essentially the same as textDidChange. Assuming we don't use a new VC to display the search results, I can't see why one should add UISearchController on top of UISearchBarDelegate.

From the documentation for UISearchController:
You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.
Each search controller provides a UISearchBar object that you must incorporate into the user interface of your initial view controller. Add this object to the view containing your searchable contents. When the user taps the search bar to enter a search term, the search controller automatically displays your search results view controller and notifies your app that the search process has begun.
Basically, the search controller handles the grunt work of displaying the search results controller, animating the search bar to the header, graying out the results if there is nothing entered, etc.. It does a lot of the grunt work for you when switching between your regular view controller and the results controller.
When you don't need (or want) all of that helpful work done for you and you want to show your own search bar, or your main view controller can easily show the search results, then you don't need to use a UISearchController.
This is similar to asking why you should use a UINavigationController when you can add your own UINavigationBar and delegate or why use a UITabBarController when you can add your own UITabBar and delegate. It's all a matter of convenience and user interface consistency.

Related

UISearchController ResultsController appearing over navigation bar

I have a UISearchController which has it's own custom results controller, rather than filtering the contents in the immediate view.
The UISearchBar appears in the navigation bar correctly, but when I begin typing characters into the search box, my custom controller appears, and fills the entire screen, covering up the navigation bar and the window I'm typing my query into.
This used to work, but I recently changed my storyboard to have the UINavigationController as the entry point, leading into a UITabBarController, which then had it's tabbed controllers.
The UITabBarController now has all the logic for the searches, so it can route the actions taken on the search results to the appropriate tab via a protocol I've implemented.
Below is my storyboard, note, that the custom search result controller is the item sitting below the UITabBarController, and the unattached item on the top right corner, is used via a library and is attached to the top tab's controller internally.
I'm starting to wonder if this specific configuration works? Can I have the UINavigationController in front of the UITabBarController?
I wanted to have a persistent search bar up top with all the shared search code in one spot (UITabBarController) rather than put a UINavigationController in front of each tab?
If this should work, are there any ideas why the custom search results controller is now covering the entire screen, instead of going under the navigation bar?
Turns out the issue was I needed to call
definesPresentationContext = true
In the view controller that was already being displayed (tab 1)
When I moved the search code over to the UITabBarController, I moved that with it.
I believe you just need to have
edgesForExtendedLayout = []
Inside your SearchResultsControllers ViewDidLoad()
Should now be under the searchController searchBar.

Why does a segmented control hide when using a TabBarController?

I want to use a segmented control inside a tab of a UITabBarController on iOS. It seems to work without it, but as soon as I embed it in a TabBarController, the segmented control won't show up in the navigation bar.
Am I missing something or is it just not supported because of some UI-guidelines? I haven't found anything in the Apple Design Guidelines...
This is the working version:
But in this setup, the segmented control does not show up:
First question is: What are you trying to achieve - what interface do you want to provide?
It is very uncommon to have a tab bar controller embedded in a navigation controller.
A tab bar controller is meant to be used as main app navigation. You can find it in so many Apple Apps (Music, Phone etc.)
These Apps have a tab bar controller with multiple navigation controllers. For example your first tab is a navigation controller with a normal view controller as root.
In this controller you then can set you segmented control.
Apple describes this behavior in it's Combined View Controller Interfaces Documentation (https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html#//apple_ref/doc/uid/TP40011313-CH6-SW1).
Update
#kaushal answer solves your problem ... yes. But if you will try to style the navigation bar for every view controller contained in your tab view controller (maybe you want to add a button as rightBarButtonItem only for one view controller) this won't work.
If you want a segmented control on every controller of the tab view controller just isolate the code for this control and reuse it in every controller.
This would achieve the same effect but you would have a cleaner software design.
try this :
Navigation bar is common throwout the stack, It will load once. And it was empty for first view. If you want it customisable for particular VC then you have to do it programatically by accessing self.NavigationBar in view did load method.

Smooth UISearchController navigation bar transition

I have a navigation controller with a root view controller that sets its titeView to a UISearchBar. Upon submitting their query, I push on another view controller which also has a UISearchBar populated with their query. If possible, I would like to keep this search bar in place and just transition the view controller below it. Is this possible with a navigation controller? Or do I need to make a custom container view controller.
One thing you can do is create a new custom view & do transition animation to the custom view upon submitting the query. This way will keep UISearchBar stay in place.

Advantage of using tabbarcontroller instead of tab bar and tab bar item

This may be a completely meaningless question, but I got lost. Why should one use tabbarcontroller instead of using tab bar and tab bar items?
As can be seen in the image, I can put a Tab Bar and Tab Bar Items inside a root view, so why should I use tabbarcontroller?
You use a UITabBarController when you want a simple way to switch between different view controllers using a tab bar.
You use a UITabBar when you want your own custom behavior attached to the tab bar.
A view (UITabbar) object knows how to display data to a user and accept user input. UITabBar is a subclass of UIView, display a list of tabs items to the user, and how to display feedback to the user when the user interacts with the tabs. UITabItems are similar to any other UIElements but specially designed to be used within UITabbar due to the unique design and property such as badges.
A controller (UITabbarController) object that knows what data to display to a user and what to do with user input. A UITabBarController is a subclass of UIViewController, It knows what tabs to display to the user, and what to do when the user chooses a tab. This class acts as a convenient base class from which you can derive your controller and also has property's that make it easier to reference when developing.

Search Display Controller tableview

I have a Search Bar and Search Display controller that I dragged from the Objects list on the right of Interface Builder. I placed this object on my DetailViewController's view. I set the delegate to be the DetailViewController. I have code in the DetailViewController to supply an NSArray of searched results to the tableview that shows up when text is typed in the search bar. Basically, the search bar is on top of the DetailView of a Master/Detail view....
Everything is working fine except how do I get access to the tableview so that I can graphically design the tableview and cells in the Interface Builder storyboard? I would like as much as possible to avoid designing views programmatically.
The search display controller's table view doesn't exist until your app runs and the search takes place! So there's nothing to design until then. You can't set the search display controller's searchResultsTableView; you can only read it, so obviously you'll have to do that in code.

Resources