Add Image on TableViewController till Search bar is inactive - ios

I am trying something and don't know if I am on a right track.
I have a UITableViewController added to ViewController. My table have multiple sections and I would like my table to be empty till the UISearchBar is inactive. Which I have already accomplished.
I would like to add multiple images to the blank screen in the bottom till the search bar is active, so the screen is not blank.
How do I add images to this screen till I click the search bar and hide the images once I click the search bar and populate the table.

Add imagesViews to your view controller's view. Set your search bar's delegate to your view controller subclass. Then say something like:
func searchBarTextDidBeginEditing(searchBar: UISearchBar){
self.imageView.hidden = true
}
And
func searchBarTextDidEndEditing(searchBar: UISearchBar){
if searchBar.text.length() == 0{
self.imageView.hidden = false
}
}

Related

Gap above first table with using UISearchController

I am not able to remove gap over first table row when testing with iOS 10.3. I tried all solutions suggested on other posts like automaticallyAdjustsScrollViewInsets, added a dummy view above UITableView in hierarchy, e.g. here - but all without success.
Here is the setup and scenario.
I have a UIViewController that has UITableView. Calling it V1. it is embedded in a nav controller.
I am adding UISearchController in viewDidLoad() to tableview header. I am not setting any constraints not controlling any layout programatically.
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = true
if #available(iOS 11.0, *) {
self.searchController.searchBar.delegate = self
self.navigationItem.searchController = self.searchController
self.navigationController?.navigationBar.prefersLargeTitles = true
self.searchController.searchBar.scopeButtonTitles = ["All","A","B"]
self.navigationItem.hidesSearchBarWhenScrolling = true
} else {
self.searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = ["All","A","B"]
tableView.tableHeaderView = searchController.searchBar
self.searchController.searchBar.sizeToFit()
}
I have another VC (a table) from which i capture data using unwind segue. Calling it V2.
i do the below in unwind method
DispatchQueue.main.async {
self.searchController.searchBar.text = searchText
self.searchController.isActive = true
let scope = self.searchController.searchBar.scopeButtonTitles![self.searchController.searchBar.selectedScopeButtonIndex]
self.filterContentForSearchText(searchText, scope: scope)
}
Scenario 1 - works fine
1. Opening V1 displays rows and search bar accurately in table header.
2. Tap on bar button that opens V2
3. Select a value and return
4. Returned value is assigned to search bar and search bar opens in navigation bar
5 Table shows rows that matches returned value from V2
There is no gap between first row and search bar
Scenario 2 - that fails.
1. Opening V1 displays rows and search bar accurately in table header.
2. Scroll the rows up such that header goes under navigation bar
3. Tap on bar button that opens V2
4. Select a value and return
5. Returned value is assigned to search bar and search bar opens in navigation bar
5 Table shows rows that matches returned value from V2
issue in this scenario is gap appear between first row and search bar.
if i tap cancel button on search bar then search bar is restored on top of table but there is gap between searchbar ie header row and navigation bar.
I noticed that tableview's origin is -88 when above happens.
i tried setting table view top constraint to both superview ie View and bottom of top layout guide.
What could be the issue? screenshots attached.
Screenshot showing gap between active search controller and first cell.
Screenshot showing gap between inactive search controller and first cell.
Visual debugger showing ViewController view in orange, tableview in green and header view in flat plum. There is a view above first cell that i have selected. Don't know what it is but is it causing the problem? A view there at bottom table also.
Thanks
Ashish

Search bar in title view not centered anymore when becoming first responder

In my navigation bar, I have a right bar button item representing a search button. When I press on it, the following function gets called
func didTapSearchButton(sender: Any) {
navigationItem.rightBarButtonItems?.removeLast()
navigationItem.titleView = searchController.searchBar
self.searchController.searchBar.becomeFirstResponder()
}
The problem with the following code is that the search bar added in the title view is not centered, i.e it's not aligned with the back button.
Something interesting that I've noticed is that if I comment out the call self.searchController.searchBar.becomeFirstResponder(), my title view stays centered.
Can anyone help?

SearchController changes the size of tableView

The size of my tableView mysteriously changes heights when the search controller is active like the images I attached below.
The image in the far left is the initial state. Middle image is when the search controller is active and the last image is when the search controller is dismissed.
I tried setting the content size of the tableview when the search controller is active, in the viewDidLoad, viewWillAppear and viewDidAppear without any luck.
Any idea on how to resolve this problem?
P.S. The items you see are just dummy posts.
Try set edgesForExtendedLayout property to be under Top Bar using code
or storyboard
I solved it by setting the extendedLayOut to 0
self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
However, the status bar was covering the search bar so I hide the status bar when the searchController is active
override var prefersStatusBarHidden: Bool {
return searchController.isActive
}

Navigation controller's toolbar not being hidden after enabling hide on tap?

I have a navigation controller where I have enabled hide on tap.It hides at first when I tap on the screen but when I tap again,the nav bar hides but the toolbar does not hide at all and it is obstructing my view. I have already tried settoolbarhidden and toolbar.hidden properties but it does not work.How do I solve this?
EDIT : I need to hide it only on this screen,I need the toolbar for other screens so thats why I have enabled shows toolbar.
EDIT 2 : Let me frame my question better.
When I enter the view controller :
Both navbar and toolbar hides because I have set it to hidden which is good
When I tap the screen :
Both navbar and toolbar shows because I have set it this way in the previous view controller.(If possible,Can I only show/hide the navigationbar on tap not the toolbar?
And lastly when I tap it again to hide both bars :
The navigation bar hides but the toolbar does not go away? This is my problem.
As Per your question you want to show tool bar on a particular viewController. View Controller viewWillAppear Function Hide ToolBar and viewDidDisappear show your tool bar it will show on other view controllers.
" Please check the navigation controller checkbox its disable or not.After that set this on your view controller before your profile view controller "
override func viewWillAppear(animated: Bool) {
self.navigationController?.toolbarHidden = true;
}
override func viewDidDisappear(animated: Bool) {
self.navigationController?.toolbarHidden = false;
}
I think it will resolve your issue.
I had the same problem.
The hideBarsOnTap only work if you placed smth in it. So if it is empty it will stay.
You could just put a blank imageView or Label there for example.
Or if you want it completely blank, your only option is to put a tabGestureRecognizer on your View!

Swift cant use hidesBarsOnSwipe because of embedded table view

In my VC I have a container view that holds an embedded table view.
My problem now is that I cant use navigationController?.hidesBarsOnSwipe = true
In my VC since it won't pick up swipes from the embedded table view. I have also tried to set hidesBarsOnSwipe = true in my table views VC but I can't catch that in VC1.
I have also tried to make deletages in my table view that fires once I scroll up or down and then I hide the navigation bar. But the problem there is that I can't get it to stay hidden or visible since the scroll has a bounce effect / pull to refresh = will hide/show my nav bar a few times in a row since its bouncing. And I don't want to remove the bounce effect.
So is there any way to detect navigationController?.hidesBarsOnSwipe = true in an embedded table view?
You can add a gesture recognizer, and in its delegate return true in method called func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWith: UIGestureRecognizer) -> Bool to make tableView work alongside with GestureRecognizer. After that you can handle swipes to hide or show navigationBar whenever you wan.

Resources