交互式键盘总是弹跳时遇到问题 - 它也在弹跳集合视图的背景



所以我有一个带有消息的交互式键盘。很好,效果很好。直到我需要一张图像作为背景。我设置映像的方式是这样的:

 let bgImage = UIImageView();
 bgImage.image = self.image
 self.collectionView.backgroundView = bgImage

我的键盘滚动和交互式的代码是这样的

 //(In viewdidload)  ....
self.collectionView.bounces = true
self.collectionView.alwaysBounceVertical = true //Scrolling
setupKeyboardObservers()
}
    func setupKeyboardObservers(){
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
}
  override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}
@objc func handleKeyboardDidShow(){
    if messages.count > 0{
        let indexPath = NSIndexPath(item: messages.count-1, section: 0)
        collectionView?.scrollToItem(at: indexPath as IndexPath, at: .top, animated: true)
    }
}

出于某种原因,这也在移动我的收藏视图的背景图像,我想这是有道理的。我该如何解决这个问题?我似乎找不到任何其他方法来在不移动的情况下为 collectionview 设置背景:( 谢谢!

  • 是的,这是因为您已将图像添加到集合视图背景。
  • 相反,您应该先将其添加到视图中,然后在其顶部添加集合视图并使集合视图背景清晰,然后它不会滚动图像。

最新更新