检查 this video准确理解我的问题。
我有一个自定义 View 组合,其中包含一个 UICollectionView,用户可以在其中使用两个向上/向下按钮进行滚动。这些值在更改时应该居中,一切都很好,直到我返回到第一个看起来比中心低得多的值。请注意,我对水平 ScrollView 使用相同的方法没有任何问题。
我的值居中方法是从 X 轴居中复制而来的,只是更改了 Y 轴的相关部分。
创建了一个UICollectionViewFlowLayout派生类,并设置为UICollectionView的UICollectioViewFlowLayout。请注意,以编程方式滚动时不会调用覆盖方法。
[Register("CenterVerticalCellCollectionViewFlowLayout")]
public class CenterVerticalCellCollectionViewFlowLayout : UICollectionViewFlowLayout
{
public CenterVerticalCellCollectionViewFlowLayout(IntPtr ptr) : base(ptr)
{
}
public override CGPoint TargetContentOffset(CGPoint proposedContentOffset,
CGPoint scrollingVelocity)
{
var cv = this.CollectionView;
var cvBounds = cv.Bounds;
var halfHeight = cvBounds.Height * 0.5;
var proposedContentOffsetCenterY = proposedContentOffset.Y + halfHeight;
var attributesForVisibleCells = LayoutAttributesForElementsInRect(cvBounds);
UICollectionViewLayoutAttributes candidateAttributes = null;
foreach (var attributes in attributesForVisibleCells)
{
// == Skip comparison with non-cell items (headers and footers) == //
if (attributes.RepresentedElementCategory != UICollectionElementCategory.Cell)
{
continue;
}
if ((attributes.Center.Y == 0) ||
(attributes.Center.Y > (cv.ContentOffset.Y + halfHeight) &&
scrollingVelocity.Y < 0))
{
continue;
}
// == First time in the loop == //
if (candidateAttributes == null)
{
candidateAttributes = new UICollectionViewLayoutAttributes();
}
else
{
candidateAttributes = attributes;
continue;
}
var a = attributes.Center.Y- proposedContentOffsetCenterY;
var b = candidateAttributes.Center.Y - proposedContentOffsetCenterY;
if (Math.Abs(a) < Math.Abs(b))
{
candidateAttributes = attributes;
}
}
// Beautification step , I don't know why it works!
if (proposedContentOffset.Y == -(cv.ContentInset.Top))
{
return proposedContentOffset;
}
if (candidateAttributes == null)
{
candidateAttributes = new UICollectionViewLayoutAttributes();
}
return new CGPoint(Math.Floor(candidateAttributes.Center.Y -
halfHeight), proposedContentOffset.Y);
}
}
加载自定义 UIView 时,我设置如下。
CollectionView.ContentOffset = CGPoint.Empty;
在 UIViewController.ViewWillLayoutSubviews
中,我调用了以下方法。
CollectionView.CollectionViewLayout.InvalidateLayout();
在 UIViewController.ViewDidLayoutSubviews
中,我调用了下面的内容。
var insets = CollectionView.ContentInset;
var value = (View.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;
insets.Top = (nfloat)value;
insets.Bottom = (nfloat)value;
CollectionView.ContentInset = insets;
CollectionView.DecelerationRate = UIScrollView.DecelerationRateFast;
最后是下面以编程方式向上/向下滚动的代码。
partial void UpButtonTouchUpInside(UIButton sender)
{
MoveSelectionCollectionViewCell();
}
partial void DownButtonTouchUpInside(UIButton sender)
{
MoveSelectionCollectionViewCell(false);
}
void MoveSelectionCollectionViewCell(bool moveUp = true)
{
var firstVisibleIndexPath = CollectionView.IndexPathsForVisibleItems.FirstOrDefault();
if (firstVisibleIndexPath != null)
{
var row = moveUp ? firstVisibleIndexPath.Row + 1 : firstVisibleIndexPath.Row - 1;
if (row > -1 &&
row < _source.Collection.Count)
{
var scrollAtIndexPath = NSIndexPath.FromRowSection(row, 0);
CollectionView.ScrollToItem(
scrollAtIndexPath,
UICollectionViewScrollPosition.CenteredVertically,
true);
}
}
}
请参阅下面的 UICollectionView IB 属性。
最佳答案
在回顾我的问题时让我意识到了问题所在!
当在UIViewController.ViewDidLayoutSubviews
中调用代码时,它会考虑View.Frame.Height
,这是错误的,因为自定义控件View的大小与UICollectionView
因此证明了这个问题。
改变这个:
var value = (View.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;
对此:
var value = (CollectionView.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;
成功了。
关于ios - 返回时,UICollectionView 垂直流不会使第一个单元格居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452842/