Navigation bar disappears if reload data with UISearchController

不打扰是莪最后的温柔 提交于 2020-01-13 13:05:52

问题


My application works so that I have a UISearchController in a reusable view inside a UICollectionViewController. When tap in the search bar I click any of the results, and it triggers a notification to reload the collectionView in order to reflect the changes.

lazy var searchController: UISearchController = {

    let tableViewController = UITableViewController(style: .Plain)
    tableViewController.tableView.dataSource = self
    tableViewController.tableView.delegate = self
    tableViewController.tableView.registerClass(StoreListCell.self, forCellReuseIdentifier: "MiniStoreCell")

    let searchController = UISearchController(searchResultsController: tableViewController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(self.collectionView.frame), height: 44)
    return searchController
    }()

 override func viewDidLoad() {
    super.viewDidLoad()
    definesPresentationContext = true

    collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SearchView")
    let layout = collectionView.collectionViewLayout as UICollectionViewFlowLayout
    layout.headerReferenceSize = CGSize(width: CGRectGetWidth(collectionView.frame), height: 44)

    token = RLMRealm.defaultRealm().addNotificationBlock({ [unowned self] (notification, realm) -> Void in
      self.categories = Category.allObjects().sortedResultsUsingProperty("name", ascending: true)
      self.collectionView.reloadData()
    })
  }
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let searchView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SearchView", forIndexPath: indexPath) as UICollectionReusableView
    searchView.addSubview(searchController.searchBar)
    searchController.searchBar.sizeToFit()
    return searchView
  }

Since the reloadData reloads also the reusable view where the search bar resides, this causes the navigation bar to disappear, leaving me stuck in the searchresultsController

Before

After

来源:https://stackoverflow.com/questions/27098127/navigation-bar-disappears-if-reload-data-with-uisearchcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!