Search bar issue ios 11, refresh control - ios

Im adding refresh control to tableView.
then start animate it from code dosent show on top. I must scroll tableView to down.
And after this i add Search controll to navbaritem then its look like:
here is code
lazy var customRefreshControl: UIRefreshControl = {
let control = UIRefreshControl()
control.attributedTitle = NSAttributedString(string: "Downloading Locations")
control.addTarget(self, action: #selector(updateLocation), for: .valueChanged)
return control
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .automatic
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.tintColor = .white
searchController.searchBar.delegate = self
tableView.addSubview(customRefreshControl)
title = "location_groups".localized()
tableView.tableFooterView = UIView()
tableView.backgroundColor = .groupTableViewBackground
navigationItem.searchController = searchController
searchController.dimsBackgroundDuringPresentation = false
}
func loadLocationGroups() {
DispatchQueue.global(qos: .userInteractive).async { [unowned self] in
self.locationGroups = DataBaseManager.Instance.GetLocations()
DispatchQueue.main.async {
self.tableView.reloadData()
if self.locationGroups.isEmpty {
self.customRefreshControl.attributedTitle = NSAttributedString(string: "Downloading Locations")
} else {
self.customRefreshControl.attributedTitle = NSAttributedString(string: "Synchronize")
}
self.customRefreshControl.beginRefreshing()
}
self.request(requestType: .getLocationGroups)
}
}

Related

Swift - Cannot pass value successfully to the ViewController that is presented next

I have an ItemsController and I want to pass a value to the RepostController when using self.navigationController?.pushViewController(destination, animated: true) to present the RepostController. I want to set the itemId of the RepostController to be cell.item!.getId(). But in the RepostController, I always get a value of 0 from the itemId. I have also tried to print the values. They are commented in the codes below.
I'm using Swift 4 with Xcode 10.1.
// ITEMS CONTROLLER
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)]
self.navigationController?.navigationBar.tintColor = UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)
self.navigationController?.navigationBar.shadowImage = UIImage()
tableView.estimatedRowHeight = 250
tableView.rowHeight = UITableView.automaticDimension
DispatchQueue.main.async() {
self.refresh(sender: self)
self.tableView.reloadData()
}
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
}
if ((profileId == iApp.sharedInstance.getId() && pageId == Constants.ITEMS_PROFILE_PAGE) || pageId == Constants.ITEMS_FEED_PAGE || pageId == Constants.ITEMS_STREAM_PAGE) {
let newItemButton = UIBarButtonItem(barButtonSystemItem: .add , target: self, action: #selector(newItem))
self.navigationItem.rightBarButtonItem = newItemButton
}
// add tableview delegate
tableView.delegate = self
tableView.dataSource = self
self.tableView.tableFooterView = UIView()
self.refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
self.tableView.addSubview(refreshControl)
self.tableView.alwaysBounceVertical = true
// prepare for loading data
self.showLoadingScreen()
// start loading data
self.loadData()
}
func showRepostButton(cell: ItemCell) {
let storyboard = UIStoryboard(name: "Content", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "RepostController") as! RepostController
destination.itemId = cell.item!.getId()
print (String(cell.item!.getId())) // Whatever the correct value is (such as 20)
self.navigationController?.pushViewController(destination, animated: true)
}
// REPOST CONTROLLER
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)]
self.navigationController?.navigationBar.tintColor = UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)
textView.delegate = self
textView.tintColor = UIColor(red:0.23, green:0.72, blue:0.65, alpha:1.0)
print ("itemID:" + String(self.itemId))
}
#IBAction func cancelTap(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func repostTap(_ sender: Any) {
self.message = self.textView.text
send()
}
I need the value to be passed to the RepostController successfully.
What changes do I need to make? Thanks!
What I would do is create a PUSH segue in IB. Then in the prepare(forSegue:) method:
if let dest = segue.destination as? YourDestinationVC {
dest.itemId = someValue
}

How do I avoid the weird gap between search bar and the table view while search results displaying?

Check: Screenshot
I tried solutions to all relevant questions. However none of them helped really.
I am getting a weird gap between table view and search bar when search bar shows results.
Help me find out what's wrong with my code:
the view controller subclasses from UITableViewController, UISearchBarDelegate, UISearchResultsUpdating
var searchController = UISearchController()
var resultsController = UITableViewController()
var refreshController = UIRefreshControl()
in override :
configureSearchController()
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
refreshController.attributedTitle = NSAttributedString(string: "")
refreshController.addTarget(self, action: #selector(refreshSelector), for: .valueChanged)
tableView.addSubview(refreshController)
searchController.searchBar.delegate = self
automaticallyAdjustsScrollViewInsets = false
definesPresentationContext = true
#objc func refreshSelector()
{
if(!searchLoaded)
{
searchLoaded = true
self.tableView.tableHeaderView = searchController.searchBar
self.navigationItem.rightBarButtonItem = nil
print( "Got ya")
}
refreshController.endRefreshing()
}
func configureSearchController ()
{
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = self
searchController.searchBar.layer.borderWidth = 1;
searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
searchController.searchBar.layer.borderColor = UIColor.searchBarBackgroundGrey().cgColor
self.navigationController?.navigationBar.shadowImage = UIImage()
}
deployment target has been set as 9.3

How to disable cancel button of UISearchController.seachBar when i tap at searchBar?

i want to disable right cancel button when a tap at search Bar.
Because of using Google Place search i put i should use searchController?.searchBar
I try to disable cancel button at
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
but when i tap a searchBar i see how cancel button appear and disappear, thats looks ugly
Please give me advice!
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.sizeToFit()
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.placeholder = searchBarPlaceholderText
searchController?.searchBar.tintColor = #colorLiteral(red: 0.1019607843, green: 0.5490196078, blue: 1, alpha: 1)
searchController?.searchBar.delegate = self
searchController?.delegate = self
searchController?.searchBar.searchBarStyle = .prominent
definesPresentationContext = true
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
checklocationAuthorizationStatus()
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
locationManager.startUpdatingLocation()
}
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
You can create a custom class and subclass UISearchBar and UISearchViewController.
For example:-
class CustomizedSearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
Now Create Object of the CustomizedSearchBar and use it within other viewController.
Or you can create a customized searchViewController as follows:
class CustomizedSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let result = CustomSearchBar(frame: CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Please follow this link for more detail information.

Search Controller move along tableview

code of search controller
it's working fine for searching
issue 1:
when I scroll tableView, I want to show search controller along with table view, search controller also gets scrolled with table view.
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.searchBar.sizeToFit()
self.searchController.searchBar.delegate = self
tableView.tableHeaderView = nil
definesPresentationContext = true
}
update data of searching :
func updateSearchResults(for searchController: UISearchController) {
_ = kidsData
let searchToSearch = searchController.searchBar.text
if(searchToSearch == "")
{
self.kidsData = self.KidsDataDuplicate
}
else{
self.kidsData.removeAll()
let itemsarray = self.KidsDataDuplicate
var forkidsinArray = [String]()
for Kids in itemsarray {
forkidsinArray.append(Kids.name)
if(Kids.name.range(of: searchToSearch!, options: .caseInsensitive) != nil)
{
self.kidsData.append(Kids)
}
}
}
self.tableView.reloadData()
}
issue 2 :
Tableview navigation bar contains search button, when I click on search button at navigation bar, search controller should show its working at tableview header, but after scrolling tableview I want to show search controller at tableview starting cell.
I can try the scroll delegates it's not working for me
pls help me......!
this is code of search button at navigation
var launchBool: Bool = false {
didSet {
if launchBool == true {
Status = 1
tableView.tableHeaderView = searchController.searchBar
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
} else {
tableView.tableHeaderView = nil
myInt = 0
}
}
}
#IBAction func NAVSearchButton(_ sender: UIBarButtonItem) {
launchBool = !launchBool
}

Adding UISearchController to UIView

I'm trying to add a UIViewSearchController to ad UIView however it does not seem to show? So far I've created a subclass of UISearchController and UISearchBar. I've done that to remove the cancel button, from the searchController. However since i added these custom classes the searchBar does not show in the View? How come this that?
viewDidLoad in viewcontroller
//SearchController
backSearchView.backgroundColor = UIColor.clearColor()
self.resultSearchController = ({
let controller = MySearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.frame = CGRectMake(8, 8, self.view.frame.width-16, 44)
controller.searchBar.setShowsCancelButton(false, animated: false)
controller.searchBar.backgroundImage = UIImage()
controller.searchBar.placeholder = "Søg"
controller.searchBar.returnKeyType = UIReturnKeyType.Default
controller.searchBar.delegate = self
let textFieldInsideSearchBar = controller.searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.whiteColor()
textFieldInsideSearchBar?.clipsToBounds = true
textFieldInsideSearchBar?.layer.cornerRadius = 1
textFieldInsideSearchBar?.layer.borderWidth = 2
textFieldInsideSearchBar?.layer.borderColor = UIColor.whiteColor().CGColor
textFieldInsideSearchBar?.textColor = UIColor.darkGrayColor()
backSearchView.addSubview(controller.searchBar)
return controller
})()
SearchController subclass
class MySearchController: UISearchController, UISearchBarDelegate {
lazy var customSearchBar: NoCancelSearchBar = {
[unowned self] in
let result = NoCancelSearchBar(frame:CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return customSearchBar
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
SearchBar subclass
class NoCancelSearchBar: UISearchBar {
override func layoutSubviews() {
self.showsCancelButton = false
}
}

Resources