【问题标题】:Prevent Search bar from resigning being first responder SWIFT 4.2防止搜索栏辞去第一响应者 SWIFT 4.2
【发布时间】:2020-04-08 15:02:09
【问题描述】:
【问题讨论】:
标签:
uisearchbar
collectionview
swift4.2
reloaddata
becomefirstresponder
【解决方案1】:
我找不到任何合适的解决方案来解决这个问题,所以我将搜索栏添加到我的主视图中,并将其高度值与我的集合视图 didscroll 事件同步。
如果有人感兴趣,这里是代码sn-p。
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?