
我一直在尝试创建一个可以重用的聊天界面.我差不多完成了实现,但有些事情让我不禁烦恼.如果我在第一次加载界面时开始加载像gif这样的消息,你可以看到在第4条消息之后有3条消息没有滚动到底部.第8个是第一个最终滚动的.这取决于屏幕尺寸.在iPhone 6s测试设备上,它到达第9条消息,即滚动的消息.
我正在使用内容插入作为保持集合视图可见的方法,每次底部的UIToolbar框架更改时运行以下代码
toolbar.inputAccessoryVIEwFrameChanged = {(rect: CGRect) in VoID() let navigationAndStatusHeight = self.navigationController != nil && self.navigationController!.navigationbar.isTranslucent ? self.navigationController!.navigationbar.frame.size.height + UIApplication.shared.statusbarFrame.height : 0 self.collectionVIEw.contentInset = UIEdgeInsets(top: navigationAndStatusHeight + 8,left: 8,bottom: UIScreen.main.bounds.height - rect.origin.y + 8,right: 8) self.collectionVIEw.scrollindicatorInsets.bottom = UIScreen.main.bounds.height - rect.origin.y} 每次插入新消息时都会运行此代码:
func insertNewMessage(){ self.collectionVIEw.performBatchUpdates({ self.collectionVIEw.insertItems(at: [NSIndexPath(item: self.numberOfMessages() - 1,section: 0) as IndexPath]) }) { (Bool) in self.scrollToBottom(animated: true) }} scrollToBottom函数是:
func scrollToBottom(animated: Bool){ guard self.numberOfMessages() > 0 else{ return } self.collectionVIEw.scrollToItem(at: IndexPath(item: self.numberOfMessages() - 1,section: 0),at: UICollectionVIEwScrollposition.top,animated: animated) } 我目前正在运行此版本的XCode版本8.1 beta(8T29o)& iOS 10.1(14B55c)
解决方法 问题可能是当集合视图内容大小太小时,scrollToItem无法正常工作.尝试使用此代码func scrollToBottomAnimated(animated: Bool) { guard self.collectionVIEw.numberOfSections > 0 else{ return } let items = self.collectionVIEw.numberOfItems(inSection: 0) if items == 0 { return } let collectionVIEwContentHeight = self.collectionVIEw.collectionVIEwLayout.collectionVIEwContentSize.height let isContentTooSmall: Bool = (collectionVIEwContentHeight < self.collectionVIEw.bounds.size.height) if isContentTooSmall { self.collectionVIEw.scrollRectToVisible(CGRect(x: 0,y: collectionVIEwContentHeight - 1,wIDth: 1,height: 1),animated: animated) return } self.collectionVIEw.scrollToItem(at: NSIndexPath(item: items - 1,section: 0) as IndexPath,at: .bottom,animated: animated) } 总结 以上是内存溢出为你收集整理的ios – UICollectionView在前几个项目上没有滚动到底部全部内容,希望文章能够帮你解决ios – UICollectionView在前几个项目上没有滚动到底部所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)