【问题标题】:UiSearchController to UiCollectionView in SwiftSwift 中的 UiSearchController 到 UiCollectionView
【发布时间】:2018-09-19 20:48:11
【问题描述】:

我想以编程方式添加搜索栏。我希望它看起来像在设置应用程序中(向下滚动时出现,向上滚动时消失)。

但是搜索栏没有出现。

大纲:

视图控制器:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {

    var filtered:[String] = []
    var searchActive : Bool = false
    let searchController = UISearchController(searchResultsController: nil)

    @IBOutlet weak var collectionView: UICollectionView!

    var items = ["Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange", "Apple", "Orange"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self

        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true
        self.searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search..."
        searchController.searchBar.sizeToFit()
        searchController.searchBar.becomeFirstResponder()
        self.navigationItem.titleView = searchController.searchBar

        self.definesPresentationContext = true
        self.searchController.searchBar.placeholder = "Search for Items"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            if searchActive {
                return filtered.count
            }
            else
            {
                return items.count
            }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        cell.contentView.layer.cornerRadius = 7.0
        cell.contentView.layer.borderWidth = 1.0
        cell.contentView.layer.borderColor = UIColor.clear.cgColor
        cell.contentView.layer.masksToBounds = false
        cell.layer.cornerRadius = 7.0

        return cell
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false
        self.dismiss(animated: true, completion: nil)
    }

    func updateSearchResults(for searchController: UISearchController)
    {
        let searchString = searchController.searchBar.text

        filtered = items.filter({ (item) -> Bool in
            let countryText: NSString = item as NSString

            return (countryText.range(of: searchString!, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
        })

        collectionView.reloadData()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchActive = true
        collectionView.reloadData()
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false
        collectionView.reloadData()
    }

    func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
        if !searchActive {
            searchActive = true
            collectionView.reloadData()
        }

        searchController.searchBar.resignFirstResponder()
    }
}

应用程序正在运行,显示项目,但滚动时没有搜索栏。 也许这是因为我使用的是 UiCollectionView 而不是 UiTableView 并且我必须添加一些额外的代码? 我错过了什么?

视频,外观:https://drive.google.com/file/d/1P0carjjgiForBnK_UmiuBWWk5A7BqSWB/view?usp=sharing

【问题讨论】:

    标签: swift xcode uicollectionview uicollectionviewcell uisearchcontroller


    【解决方案1】:

    您是否在 iOS 11 中运行?你拉下收藏视图了吗?搜索栏仅在下拉时出现。我看到的唯一区别是,navigationItem 搜索控制器:

    self.navigationItem.searchcontroller = searchcontroller
    

    【讨论】:

    • iOS 12。我添加了该代码行,但没有任何变化。我正在拉下,但没有搜索栏。
    • 你为什么需要这个?搜索栏取消按钮?如果您向上滚动 collectionView 它应该会自动隐藏而无需添加任何代码此外 dimsBackgroundDuringPresentation 具有与 obscures 方法相同的行为,因此我将删除它
    • 我已经尝试了你的代码,它对我有用,searchBar 只是固定在导航栏中,但它可以工作......你设置你的 UInavigationController 了吗?
    • 我应该如何设置 UInavigationController?
    【解决方案2】:

    您的代码无法正常工作,因为您没有导航控制器。至少它没有显示在代码或视频中。如果不这样做,则需要将 viewController 嵌入到 navigationController 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多