I'm trying tu put a UIRefreshControl and a UISearchController into a UITableView.
When i pull down to refresh it works well but at first loading the UIRefreshControl is covered by the UISearchController:
Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl?.backgroundColor=UIColor.clearColor()
self.refreshControl!.addTarget(self, action: #selector(SocialManTableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl!.tintColor = UIColor(colorLiteralRed: 59/255, green: 89/255, blue: 152/255, alpha: 1)
self.refreshControl!.beginRefreshing()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
searchController.searchBar.barTintColor = UIColor.clearColor()
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.tintColor=UIColor.blackColor()
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: false)
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
}
Related
I've read some Stack OverFlow posts and nothing from there has worked. I have one simple problem that shouldn't be incredibly difficult to solve. I have to stop the search bar from disappearing when scrolling on the tableview that it is attached to.
I have tried:
set the tableview to .plain to make the header view sticky, but that didn't work.
I tried navigationItem.hidesSearchBarWhenScrolling = false, but this may not have worked because searchController.searchBar is set equal to tableView.tableViewHeader.
Here is my viewDidLoad, which should contain the relevant code when this works, if it is indeed possible:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.init(red: 240/255, green: 246/255, blue: 243/255, alpha: 1)
tableView.register(SearchTableViewCell.self, forCellReuseIdentifier: "SearchTVC")
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
definesPresentationContext = true
navigationController?.navigationBar.isHidden = true
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = .darkGray//UIColor.white
searchController.searchBar.barTintColor = UIColor.convertHexColor(hex: "eaeaea")
navigationItem.hidesSearchBarWhenScrolling = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.text = ""
addConstraints()
presentInitialView()
}
SearchViewController viewDidLoad has the following functionality
override func viewDidLoad() {
super.viewDidLoad()
self.cache = NSCache()
let path = "books";
self.ref = Database.database().reference().child(path);
tableViewBooks.dataSource = self
tableViewBooks.delegate = self
tableViewBooks.register(BookDetailTableViewCell.self, forCellReuseIdentifier: "book")
// Uncomment the following line to preserve selection between presentations
self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
searchController.searchBar.isTranslucent = false
let searchBarView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(45)))
let view = UIView();
view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 54)
let searchBar = searchController.searchBar
searchBar.backgroundColor = UIColor.white
searchBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 44)
searchBar.searchBarStyle = UISearchBar.Style.prominent
searchBar.placeholder = " Search book title..."
searchBar.sizeToFit()
view.backgroundColor = UIColor.white
searchBar.isTranslucent = false
searchBarView.addSubview(searchBar)
view.addSubview(searchBarView)
tableView.tableHeaderView = view
definesPresentationContext = true
tableView.separatorStyle = .none
}
When someone clicks on a row in the tableview in that view controller, they get sent to DetailController
tableRowData = filteredBooks[indexPath.row]
self.performSegue(withIdentifier: "toDetail", sender: nil)
DetailControler has a navigation bar that I can't seem to remove. I tried to add the following code to viewWillAppear, but it made no difference. What can I do?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
refresh()
}
I have tried your code and found that if you set hidesNavigationBarDuringPresentation to false, the DetailController's navigationBar will be hidden correctly. Here is my code
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
searchController.searchBar.isTranslucent = false
searchController.hidesNavigationBarDuringPresentation = false
I have a main view controller, it has a fullscreen UITableView which is populated by values from Firebase and a TabBarController below. All the code for the UITableView is handled programmatically. I need to add two options: Firstly, a search bar to query the results and a filter option for the various categories to fetch from Firebase.
Here is my updated code from the ViewController:
import UIKit
import Firebase
class PostTable: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate,UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
}
var tableView:UITableView!
var posts = [Post]()
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
if Auth.auth().currentUser == nil {
switchStoryboard()
}
tableView = UITableView(frame: view.bounds, style: .plain)
view.addSubview(tableView)
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
var layoutGuide:UILayoutGuide!
layoutGuide = view.safeAreaLayoutGuide
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.reloadData()
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false // displays tableview
let scb = self.searchController.searchBar
scb.tintColor = UIColor.white
scb.placeholder = "SEARCH"
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.green
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true
}
}
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = UIColor.green
searchController.searchBar.barTintColor = UIColor.green
}
definesPresentationContext = true
observePosts()
}
Add variable of UISearchController
var searchController : UISearchController!
& then add code in viewDidLoad
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false // displays tableview
let scb = self.searchController.searchBar
scb.tintColor = UIColor.white
scb.placeholder = "SEARCH"
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.greenColor
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true
}
}
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = UIColor.greenColor
searchController.searchBar.barTintColor = UIColor.greenColor
}
definesPresentationContext = true
An UITabbarController does not have a UINavigationController so it doesn't have a UINavigationBar.
So you could do:
Add a cell to your TableView to act like a search bar
Add a Navigationbar to the ViewController
Add a custom View to your View to act as a container
There may be more options, just want to point you into the right direction.
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.autocapitalizationType = .none
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
// Make the search bar always visible.
navigationItem.hidesSearchBarWhenScrolling = false
} else {
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = searchController.searchBar
}
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false // The default is true.
}
I'm adding a UISearchController but I keep experiencing spacing problems. In particular, when I conduct a search in the search bar, it shifts up and leaves a black space between the tableview and the search bar.
Then, when I type the the black space disappears but there's a large white space between the search bar and the table.
I tried adding the following line but the spacing problem becomes worse:
searchController.hidesNavigationBarDuringPresentation = false
Any helpful hints would be greatly appreciated.
Here is my current code:
class SearchViewController: UITableViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//Set up Table View
self.navigationController?.navigationBar.shadowImage = UIImage()
let searchResultsController = UITableViewController(style: .plain)
searchResultsController.tableView.delegate = self
searchResultsController.tableView.dataSource = self
searchResultsController.tableView.rowHeight = 65
searchResultsController.tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")
// Setup Search Controller
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.barTintColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0)
searchController.searchBar.layer.borderColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0).cgColor
searchController.searchBar.layer.borderWidth = 1.00
tableView.tableHeaderView?.addSubview(searchController.searchBar)
let searchBar = searchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}
try to Add
self.automaticallyAdjustsScrollViewInsets = false
self.extendedLayoutIncludesOpaqueBars = true
in viewDidload.
or uncheck "Adjust Scroll View Insets"
I used a navBar which is created programmatically with an UISearchController.
When I start editing the UISearchBar:
- the navBar stays behind the dim view
- the tableView hides half of the navbar
- when the tabeView appeared, the cancel buttons are not selectable.
My UISearchController :
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTableViewController") as! SearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
resultSearchController.view.backgroundColor=UIColor.clearColor()
locationSearchTable.delegate = self
let searchBar = self.resultSearchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.searchBarStyle = .Minimal
searchBar.barStyle = .Default
searchBar.translucent = true
searchBar.barTintColor = UIColor.whiteColor()
searchBar.setImage(UIImage(named:"search"), forSearchBarIcon: .Search, state : .Normal)
searchBar.delegate=self
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
My navBar :
let newNavBar : UINavigationBar = UINavigationBar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64.0))
func styleNavBar (){
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
self.navigationController?.setNavigationBarHidden(true, animated: false)
let newItem : UINavigationItem = UINavigationItem.init()
newItem.titleView = self.resultSearchController.searchBar
newNavBar.barTintColor = UIColor(red: 243/255, green: 242/255, blue: 238/255, alpha: 1.0)
newNavBar.translucent = false
if let font = UIFont(name: "Avenir-Black", size: 16.0) {
let navBarAttributesDictionary : [String : AnyObject]? = [
NSForegroundColorAttributeName: UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 0.51),
NSFontAttributeName: font
]
newNavBar.titleTextAttributes = navBarAttributesDictionary
}
newNavBar.setItems([newItem], animated: false)
self.view.addSubview(newNavBar)
self.view.bringSubviewToFront(newNavBar)
}
How can I fix it, please?
Thanks!
Try using this :
self.navigationController?.navigationBar.layer.zPosition = 1
you can use stackview for it . select search and tableview and click on stackview and constraint stackview from top/left/right/bottom