I'm trying to add an UISearchController on top of an UITableView (not in it's header) and I created a placeholder view for it in storyboard with a height constraint of 44.
In the normal state it all works fine but when I add some scope button, those overlap my first UITableViewCell and I'm unable to find a solution for this. I tried to re-set the height constraint of my placeholder view but I don't find the right functions to place it and couldn't find the "right" animation so it still looks nice.
My UISearchController looks like this:
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.searchBarStyle = .Default
self.searchController.searchBar.tintColor = UIColor.blueColor()
self.searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = ["Test1", "Test2"]
self.searchController.searchBar.sizeToFit()
self.searchBarViewWrapper.addSubview(self.searchController.searchBar)
}
Here is a solution I found worked well. Add searchBar to tableHeaderView instead of your wrapper view:
searchController.searchBar.scopeButtonTitles = ["Test1", "Test2"]
tableView.tableHeaderView = searchController.searchBar
You can download the project to see in detail
http://www.raywenderlich.com/wp-content/uploads/2015/09/CandySearch.zip
Related
Here is an image of the issue:
As you can see the text is highlighted (select all) was pressed, however, as you can see the text isn't actually highlighted.
I don't think it matters but this search bar isn't searching local data it's using Algolia.
My class in implementing UISearchBarDelegate the search bar is created in code not interface builder and the only method implemented is searchBarSearchButtonClicked which I can include code for, but I don't think it's needed. Additionally, this VC is inside a navigation controller
Code:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 135;
searchBar.placeholder = "Search"
searchBar.delegate = self
navigationItem.titleView = searchBar
tableView.delegate = self
tableView.dataSource = self
self.edgesForExtendedLayout = UIRectEdge()
self.extendedLayoutIncludesOpaqueBars = false
self.automaticallyAdjustsScrollViewInsets = false
}
The effect comes from a white selection color. You can change the selection color by assigning a color to the property tintColor of UISearchBar, or over the appearance for all search bars:
UISearchBar.appearance().tintColor = .black
SubViewController (a child of ViewController and IndicatorInfoProvider) is added using MainViewController (a child of ButtonBarPagerTabStripViewController) with NavigationBar added.
Padding does not occur even if you tap tabbutton, padding occurs on top when swipe.
animated gif 👇
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.clear
//tableview
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.backgroundView = nil
self.tableView.backgroundColor = UIColor.clear
//動的に高さを変更
self.tableView.estimatedRowHeight = 155
self.tableView.rowHeight = UITableViewAutomaticDimension
//indicator
self.tableView.showIndicator()
//loaddata
loadData(page:0)
}
I've encountered the same problem.
Open your storyboard and select the MainViewController. In the Attribute Inspector deselect the checkbox 'Adjust scroll view insets'
Are you using a UITableViewController as a childViewController? This issue seems to happen in this case.
To fix this, you should be setting automaticallyAdjustsScrollViewInsets = false and also having as childViewControllers only UIViewControllers with a UITableView inside, instead of UITableViewController.
Cheers
I added SearchBar to my TableView in ViewDidLoad() doing it:
self.searchBar = UISearchController(searchResultsController: nil)
self.searchBar.searchResultsUpdater = self
self.searchBar.dimsBackgroundDuringPresentation = false
self.searchBar.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.searchBar.searchBar
self.tableView.reloadData()
everything works fine, but when I tap on this SearchBar it disappears. It means, I can still typing, and I can see the results but, don't see SearchBar. I implemented UISearchBarDelegate and I have been trying to add
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.navigationController?.navigationBarHidden = false
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
self.navigationController?.navigationBarHidden = true
}
but it still doesn't work. Do you have any idea, why this Search Bar disappears?
solution of this problem is (like #sandy sad) write this line of code in viewDidLoad()
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
but now I have a new problem it's mean When I select row in TableView and display new VievController, SearchBar doesn't disappear and I see it in new view. Why?
You need to set extendedLayoutIncludesOpaqueBars to true in viewDidLoad().
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
Actually the search Bar is not hiding it is just adjusting it width and height according to the text.
Remove this line from your code and it will work fine.
self.searchBar.searchBar.sizeToFit()
Ran through a quick tutorial with search bars and figured that I could use searchBar.sizeToFit() to autosize the search bar, however the right end of the search bar still extends off of the screen.
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate {
var searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
}}
I've tried to manually set the frame with something like searchController.searchBar.frame = CGRectMake(0, 0, 200, 200) but the width remained off of the screen. The search bar does not exist in the storyboard, however the tableView does.
Needed to set autolayout constraints for the tableView that the search bar was a part of. Setting these fixed the sizing issues.
If you're using Auto layout, then use leading and trailing edges instead of width constraints.
Just click on Pin option given below and select top,leading(left) and trailing(right) constraints, then click on Add 3 constraints. Make sure "Constraint to margin" checkbox is unchecked. If the constraints satisfy, There will be no warning. Try changing the background color of your search bar to see its position on the screen.
If setting up auto layout constraints doesn't fix the problem, try calling searchController.searchBar.sizeToFit() in viewWillLayoutSubviews() rather than viewDidLoad(). This may help if the search bar is contained in a view other than the table header view.
Swift 4, works for all types of ViewControllers
After countless hours of trial and error here's what I came up for those who want to use it in code:
Set delegate
class SearchDatasourceController: UIViewController , UISearchBarDelegate { }
Set a customTitleView and the searchBar
IMPORTANT: set their frame
lazy var customTitleView: UIView = {
var view = UIView()
let frame = CGRect(x: 0, y: 0, width: 300, height: 44)
view.frame = frame
return view
}()
lazy var searchBar: UISearchBar = {
let searchBar = UISearchBar()
searchBar.placeholder = "Search"
searchBar.autocapitalizationType = .none
searchBar.backgroundImage = UIImage()
(searchBar.value(forKey: "searchField") as? UITextField)?.backgroundColor = UIColor(r: 230, g: 230, b: 230)
searchBar.delegate = self
searchBar.becomeFirstResponder()
let frame = CGRect(x: 0, y: 0, width: 300, height: 44)
searchBar.frame = frame
return searchBar
}()
in viewDidLoad add the customTitleView that holds the searchBar
override func viewDidLoad() {
super.viewDidLoad()
customTitleView.addSubview(searchBar)
navigationItem.titleView = customTitleView
searchBar.becomeFirstResponder()
}
add delegate method to listen for searches
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print(searchText)
}
make sure you present your view controller on the navigation stack
func handleSearchBarButtonItemTapped() {
let controller = SearchDatasourceController()
navigationController?.pushViewController(controller, animated: true)
}
I have a UITableView in a UIViewController...
I'm using a UISearchController to give me a search bar and setting this to the table's header view. I'm also using sections in the UITableView. My problem is, on first presentation, the table header collides with the first cell...
After search bar has been activated once and then dismissed, the table renders as I expect it to...
The code code looks like...
override func viewDidLoad() {
super.viewDidLoad()
self.table.dataSource = self
self.table.delegate = self
self.table.tableHeaderView = self.searchController.searchBar
self.definesPresentationContext = true
self.searchController.searchBar.sizeToFit()
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
}
... and this style works fine in other tables which do not have sections.
For future users, setting the table rows to have automatic height fixed this problem...
self.table.estimatedRowHeight = 44
self.table.rowHeight = UITableViewAutomaticDimension