簡體   English   中英

如何實現將tableView滾動到頂部,將搜索欄隱藏在其tableheaderView中?

[英]How to implement scrolling of the tableView to the top hiding the search bar in its tableheaderView?

我已經經歷過一些類似的問題,並嘗試實施其解決方案。 但是,尤其是在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)

    }




}

您應該在viewDidLoad()中添加definesPresentationContext = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM