我正在尝试使用contentOffset隐藏作为UICollectionView的子视图添加的UISearchController,该子视图用作UICollectionView的以下答案行中的UICollectionView的节头: https://stackoverflow.com/a/7619749/7271020
var searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
collectionView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)
}
头视图是通过CollectionView委托方法注册的:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if (kind == UICollectionView.elementKindSectionHeader) {
let headerView:UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "CollectionViewHeader", for: indexPath)
headerView.addSubview(searchController.searchBar)
return headerView
}
return UICollectionReusableView()
}
我希望能够从一开始就隐藏UICollectionReusableView,并且仅在用户下拉UICollectionView时才显示它。如何实现呢?