如何实现将tableView滚动到顶部,将搜索栏隐藏在tableheaderView中?

时间:2018-08-10 06:57:04

标签: ios swift xcode

我已经经历了一些类似的问题,并尝试实施其解决方案。但是,尤其是在iPhone X上,它们似乎存在一些问题。我将部署目标设为9.3。

为此,我编写了这段代码,但iPhone X将tableView置于较高位置,同时也隐藏了第一行的一半以上以及搜索栏。

 override func viewDidAppear(_ animated: Bool) {
               self.tableView.contentOffset = CGPoint(x: 0.0,y: 0.0)

        }

以下代码段也似乎完全无效:

self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)

如果想看完整的代码,请添加。

import UIKit

class CountryTableViewController: UITableViewController, UISearchResultsUpdating {

    let countriesArray = ["Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria"]
    let  countrycodes = ["+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213"]
    var filteredArray = [String]()
    var searchController = UISearchController()
    var resultsController = UITableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        let cancelButton: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancel))
        self.navigationItem.leftBarButtonItem = cancelButton

        let searchButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(searchButtonAction))
        searchButton.image = UIImage(named: "search")
        self.navigationItem.rightBarButtonItem = searchButton

        searchController = UISearchController(searchResultsController: resultsController)
        tableView.tableHeaderView = searchController.searchBar

        searchController.searchResultsUpdater = self

        resultsController.tableView.delegate = self
        resultsController.tableView.dataSource = self

       automaticallyAdjustsScrollViewInsets = false

    }

    func updateSearchResults(for searchController: UISearchController)
    {
        filteredArray = countriesArray.filter({ (countriesArray:String) -> Bool in
            if countriesArray.contains(searchController.searchBar.text!)
            {
                return true
            }
            else
            {
                return false;
            }

        })
        resultsController.tableView.reloadData()
        automaticallyAdjustsScrollViewInsets = false
    }

    @objc func cancel() {
        self.dismiss(animated: true, completion: nil)

    }
    @objc func searchButtonAction() {
        self.dismiss(animated: true, completion: nil)
        self.searchController.isActive = true
        self.searchController.searchBar.text = ""
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == resultsController.tableView
        {
            return filteredArray.count
        }
        else
        {
            return countriesArray.count
        }

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell()

        if tableView == resultsController.tableView
        {
            cell.textLabel?.text = filteredArray[indexPath.row]
            cell.detailTextLabel?.text = countrycodes[indexPath.row]
            cell.imageView?.image = UIImage (named: "Flag0")
        }
        else
        {
            cell.textLabel?.text = countriesArray[indexPath.row]
            cell.detailTextLabel?.text = countrycodes[indexPath.row]
            cell.imageView?.image = UIImage (named: "Flag0")
        }


        return cell

    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print(indexPath.row, indexPath.section)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidAppear(_ animated: Bool) {

        self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.bottom, animated: false)
          // self.tableView.contentOffset = CGPoint(x: 0.0,y: 0.0)
          //  self.tableView.setContentOffset(.zero, animated: true)
        //self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0)
        self.automaticallyAdjustsScrollViewInsets = false;

    }
    override func viewWillAppear(_ animated: Bool) {

     //  self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)
       //   self.tableView.contentOffset = CGPoint(x: 0.0,y: 44.0)
      //  self.tableView.setContentOffset(.zero, animated: true)

    }




}

1 个答案:

答案 0 :(得分:0)

您应该在viewDidLoad()中添加definesPresentationContext = true