我的主视图控制器是一个UICollectionViewController,其初始屏幕如下:
当用户点击其中一个选项时,然后使用不同的单元格重新加载集合视图,并在顶部添加一个新的第二个UICollectionView,如下所示:
所以现在有两个集合视图,它们都是水平滚动的。顶部条用于来回切换,底部用于选择。但是,底部是启用分页并水平滚动,因此为了获得垂直效果,集合视图的单元格实际上是一个集合视图。
我的问题是,当使用SearchController时,只要显示键盘,它就会抛弃布局并弄乱底部集合视图的大小。
如何处理键盘以使我的底部集合视图保持正确格式化?我也尝试设置navigationItem.searchController = searchController,但我不希望搜索栏位于导航栏的底部,我无法解除它,所以我设置的是navigationItem.titleView = searchController.searchBar。
以下是我正在使用的一些代码:
import UIKit
class MainPageController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "mainCellId"
let secondCellId = "secondCellId"
var filteredDisplay: [String] = [String]()
var fullDisplayData: [String] = [String]()
lazy var topTabSectionBar: TopTabSectionBar = {
let cv = TopTabSectionBar()
cv.mainPageController = self
return cv
}()
var tabBarHidden: NSLayoutConstraint?
var titleView: UIView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
configureSearchBar()
setupSearchIcon()
}
func configureSearchBar() {
// Setting up the Search Controller
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
titleView = navigationItem.titleView
searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.delegate = self
}
private func setupSearchIcon() {
let searchIcon = UIImage(named: "searchicon")?.withRenderingMode(.alwaysTemplate)
let searchBarButtonItem = UIBarButtonItem(image: searchIcon, style: .plain, target: self, action: #selector(showSearch))
navigationItem.rightBarButtonItem = searchBarButtonItem
}
@objc func showSearch() {
if navigationItem.titleView == titleView {
navigationItem.titleView = searchController.searchBar
collectionView?.reloadData()
navigationController?.view.layoutSubviews()
} else {
navigationItem.titleView = titleView
collectionView?.reloadData()
navigationController?.view.layoutSubviews()
}
}
func setupCollectionView() {
collectionView?.backgroundColor = .clear
view.backgroundColor = .white
collectionView?.register(SecondCollectionView.self, forCellWithReuseIdentifier: secondCellId)
collectionView?.register(MainCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (self.tabBarHidden?.isActive)! {
return mainPageOptions.options.count
} else {
return 10
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (self.tabBarHidden?.isActive)! {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MainCell
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: secondCellId, for: indexPath) as! SecondCollectionView
if isFiltering() {
cell.displayData = filteredDisplay
return cell
} else {
cell.displayData = fullDisplayData
return cell
}
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (self.tabBarHidden?.isActive)! {
let itemSize = CGSize(width: view.frame.width, height: mainPageOptions.cvHeight)
return itemSize
} else {
let itemSize = CGSize(width: view.frame.width, height: view.frame.height - 50)
return itemSize
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DispatchQueue.main.async {
self.tabBarHidden?.isActive = false
if let flowLayout = self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 0
}
self.collectionView?.isPagingEnabled = true
self.collectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
self.collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0)
self.collectionView?.reloadData()
}
}
}
extension MainPageController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
}
}
extension MainPageController: UISearchBarDelegate {
}
我会使用任何方法来显示搜索栏,但我现在正在这样做,点击搜索图标然后显示搜索栏,会大大搞乱集合视图布局。
我的问题是:如何让搜索栏工作,根本不移动集合视图,或者正确移动它?
在显示键盘之前,它确实没有任何问题。