ios UICollectionView contentOffset 随自定义布局而变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17688207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView contentOffset changes with custom layout
提问by Evan Cordell
I have a UICollectionView with a custom UICollectionViewLayout (actually, I'm using this nice layout).
我有一个带有自定义 UICollectionViewLayout 的 UICollectionView (实际上,我正在使用这个不错的布局)。
I set contentOffset = CGPointZero
in viewDidLoad. After viewDidLoad, however, the offset is -20, and the content gets pushed down like so:
我contentOffset = CGPointZero
在 viewDidLoad 中设置。然而,在 viewDidLoad 之后,偏移量为 -20,内容被按下,如下所示:
(It should be flush with the line). I'm loading the collection view layout in interface builder. It seems that my problem is very similar to this one, however the solutions there don't work for me.
(它应该与线齐平)。我正在界面构建器中加载集合视图布局。似乎我的问题与此问题非常相似,但是那里的解决方案对我不起作用。
I tried modifying collectionViewContentSize
in my layout implementation to ensure it was always greater than the size of the collectionView. Although this means I can scroll my content down (it's shorter than the height of the collectionView) and hide the extra space, I can also scroll back up to see it.
我尝试修改collectionViewContentSize
我的布局实现以确保它始终大于 collectionView 的大小。虽然这意味着我可以向下滚动我的内容(它比 collectionView 的高度短)并隐藏额外的空间,但我也可以向上滚动以查看它。
Nothing seems to work!
似乎没有任何效果!
采纳答案by Evan Cordell
The only solution I could come up with that had barely-acceptable behavior:
我能想出的唯一解决方案是几乎无法接受的行为:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.collectionView.contentOffset.y < 0) {
self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, 0.0);
}
}
As well as setting the height of the content to fmax(self.collectionView.frame.size.height + 20, [self stackedSectionHeight])
in collectionViewContentSize
以及将内容的高度设置为fmax(self.collectionView.frame.size.height + 20, [self stackedSectionHeight])
incollectionViewContentSize
This removes the space above the section header, but it removes the "bounce" from the top. A pretty sub-optimal solution, but fairly acceptable.
这会删除部分标题上方的空间,但会删除顶部的“反弹”。一个相当次优的解决方案,但相当可以接受。
I'll accept a better answer if anyone has one, or if I find one I'll update this answer.
如果有人有一个更好的答案,我会接受一个更好的答案,或者如果我找到一个我会更新这个答案。
回答by alexey.hippie
I found the reason why this happens. Check the accepted answer from this question: Status bar and navigation bar appear over my view's bounds in iOS 7
我找到了发生这种情况的原因。检查此问题的已接受答案: 状态栏和导航栏出现在 iOS 7 中我的视图边界上
Indeed, we could just set edgesForExtendedLayout
or automaticallyAdjustsScrollViewInsets
properties of UIViewController in IB (if you use Storyboard) of in viewDidLoad
and it will fix our issue.
实际上,我们可以在 IB(如果您使用 Storyboard)的 in 中设置edgesForExtendedLayout
或设置automaticallyAdjustsScrollViewInsets
UIViewController 的属性,viewDidLoad
它将解决我们的问题。
Just don't forget check for this property is available, because in iOS6 or prior it will cause crash:
只是不要忘记检查此属性是否可用,因为在 iOS6 或更早版本中它会导致崩溃:
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
Regards!
问候!
回答by David Rees
I started to find this issue appearing on iOS 11 where I had a scrollview within a scroll view. Took me almost a day to figure it out.
我开始发现这个问题出现在 iOS 11 上,我在滚动视图中有一个滚动视图。我花了将近一天的时间才弄明白。
Try setting the content inset adjustment behaviour:
尝试设置内容插入调整行为:
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
}
This fixed my collectionview adjusting it's content inset down a small amount as the collectionview frame would move past the safe area of the enclosing scrollview.
这修复了我的 collectionview 将其内容插入少量调整为 collectionview 框架将移过封闭滚动视图的安全区域。
回答by Ankit Gabani
let oldContentSizeHeight: CGFloat = self.colName.contentSize.height
self.colName.reloadData()
self.colName.layoutIfNeeded()
self.view.layoutIfNeeded()
self.colName.contentOffset.y = self.colName.contentOffset.y + (self.colName.contentSize.height - oldContentSizeHeight)
回答by Chowdhury Md Rajib Sarwar
This is the swift 4.2 version of the correct answer but to make this workable you have to add the protocol UIScrollViewDelegate
:
这是正确答案的 swift 4.2 版本,但要使其可行,您必须添加协议UIScrollViewDelegate
:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.collectionView.contentOffset.y < 0 {
self.collectionView.contentOffset = CGPoint(x: self.collectionView.contentOffset.x, y: 0.0)
}
}