- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直在尝试创建一个可以重复使用的聊天界面。我几乎完成了实现,但有些事情一直困扰着我。如果我在第一次加载界面时像 gif 那样开始加载消息,您可以看到在第 4 条消息之后有 3 条消息没有滚动到底部。第 8 个是第一个最终滚动的。这因屏幕尺寸而异。在 iPhone 6s 测试设备上,它到达第 9 条消息,即滚动的消息。
我使用 content inset 作为保持 collectionview 可见的方法,每次底部的 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) 上运行
最佳答案
问题可能是当 collection view content size 太小时,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 没有滚动到前几个项目的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147517/
我有一个并排有两列的布局。有没有一种简单的方法可以使用单个 UICollectionView 来做到这一点?唯一的要求是该解决方案必须适用于 iOS 8,并且单元格必须在每列中垂直堆叠,如下所示:
我使用标准 UICollectionView与部分。我的单元格像网格一样排列。如果用户使用 Apple TV Remote 移动焦点,则所选方向的下一个单元格会正确聚焦。但是如果网格中有“间隙”,默认
这是我的 ImageController 类(一个 ViewController)仅显示 Collection View 的背景,而其中的单元格则不显示。有什么帮助吗?是不是我没有正确初始化什么? 这
我希望能够在 UICollectionView 中设置内容大小的最小高度,这样我就可以隐藏/显示 UISearchbar,类似于在 iBooks 上完成的方式。 但是,我不想对布局进行子类化,因为我想
有没有办法禁用 UICollectionView 的自动滚动细胞何时聚焦?当单元格进入焦点时,我想手动调整单元格的内容偏移量。 我不想更新内容偏移量: - (void)didUpdateFocusIn
如何将 UICollectionView 中的所有单元格垂直和水平居中使用 UICollectionViewFlowLayout ? 流布局根据滚动方向在右侧或底部留下间距。我想在所有方面设置相同的填
所以这是一个很无聊的问题,但我只是想不出一种非常简单的方法来检测当前关注的项目的 indexPath . 我环顾四周,希望看到一些非常简单的东西,例如 collectionView.indexPath
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController ,其中嵌入了一个 UICollectionView 。 CollectionView 的
好的,如果我有 UIViewController如果我使用这个 `preferredFocusedView ,我可以制作一个专注于 tvOS 的 View ,但是如何让 UICollectionVie
我已经构建了 2 个 Collection View ,1 个水平滚动(在顶部)和 1 个垂直滚动(在中间 - 底部),用于在我的 Objective-C iOS 应用程序中查看 2 组不同的内容,类
这就是我的问题,您在 gif 上看到的 4 个橙色矩形是单个垂直 UICollectionView = OrangeCollectionView。 绿色和紫色“卡片” View 是另一个 UIColl
大家好,我是 IOS 开发的新手,我有一个 UICollection,我在其中使用 SDWebImageCache 加载图像。问题是我的 UICollectionview 单元格在快速滚动时感觉像抽搐
我在 ViewController 中有两个 Collection View 。需要在用户滚动底部 UICollectionView 时自动滚动顶部 collectionView。 最佳答案 你可以试
我正在研究 UICollectionView,我有一个在单元格中使用 collectionView 的想法?可以吗 当我运行项目时,我收到一个错误 "InterFace Builder StoryBo
大家好,我收到以下错误 -UICollectionView 必须使用非 nil 布局参数进行初始化 PopularShotsCollectionViewController 的代码: import U
我的应用程序出现此错误: *** -[UICollectionView _endItemAnimations] 断言失败,/SourceCache/UIKit/UIKit-2372/UICollect
当我的 UIViewController 与 UICollectionView 一起出现时,内容会向上滚动一点,出现时。 我实现了 scrollViewDidScroll:我正在记录 contentO
我遇到了一个问题,在我的 UICollectionView 中,当返回的记录为 0 时,我需要显示一个页脚。下面的代码似乎运行良好,当没有记录时,显示页脚。但是,有一个问题,当有记录时,虽然foote
我是一名优秀的程序员,十分优秀!