【问题标题】:Prevent Search bar from resigning being first responder SWIFT 4.2防止搜索栏辞去第一响应者 SWIFT 4.2
【发布时间】:2020-04-08 15:02:09
【问题描述】:

我的集合视图的标题中有一个搜索栏,我希望能够根据用户类型过滤内容。问题是,每次我在搜索栏的 textDidChange 事件中重新加载集合视图时,集合视图都会成为第一响应者,并且搜索栏会辞职,从而隐藏键盘。

我已经检查了所有的答案;

UICollectionView reloadData resigns first responder in section header

How to filter UICollectionView and keep keyboard up?

以及任何其他可能相关但没有找到答案的帖子。 如果你们能提供帮助,我将非常高兴。

提前致谢

【问题讨论】:

    标签: 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?

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 2012-05-17
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多