我在集合视图的标题中有一个搜索栏,我希望能够根据用户类型过滤内容。问题是,每次我在搜索栏的textDidChange事件中重新加载集合视图时,集合视图都会成为第一响应者,并且搜索栏会退出,从而隐藏了键盘。
我已经检查了所有答案;
UICollectionView reloadData resigns first responder in section header
How to filter UICollectionView and keep keyboard up?
以及其他可能相关但没有运气找到答案的帖子。如果你们能帮助我,我会很高兴。
提前感谢
我在该问题上找不到任何合适的解决方案,因此我已将搜索栏添加到主视图中,并将其高度值与我的收藏夹视图didscroll事件同步。
如果有人感兴趣,这里是代码段。
let offset = self.collectionView.contentOffset
let y = offset.y
if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
// move up
if self.cst_searchBarHeight.constant == 0 && y <= 0{
self.cst_searchBarHeight.constant = self.searchBarHeight
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
} else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
// move down
if self.cst_searchBarHeight.constant != 0{
self.cst_searchBarHeight.constant = 0
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
// update the new position acquired
lastContentOffset = scrollView.contentOffset.y
}
我从下面的答案中得到了这个主意,它找出了收藏夹视图的滚动方向How can I detect the scroll direction from the UICollectionView?