Skip to content

Instantly share code, notes, and snippets.

@rotorgames
Last active June 23, 2022 17:39
Show Gist options
  • Select an option

  • Save rotorgames/67a83ec9bb7438aa81e7c4d905454fdb to your computer and use it in GitHub Desktop.

Select an option

Save rotorgames/67a83ec9bb7438aa81e7c4d905454fdb to your computer and use it in GitHub Desktop.
CollectionView GridViewLayout one element alignment issue
public class CustomCollectionViewRenderer : GroupableItemsViewRenderer<CustomCollectionView, GroupableItemsViewController<CustomCollectionView>>
{
ItemsViewLayout _layout;
protected override ItemsViewLayout SelectLayout()
{
var itemSizingStrategy = ItemsView.ItemSizingStrategy;
var itemsLayout = ItemsView.ItemsLayout;
if (itemsLayout is GridItemsLayout gridLayout)
_layout = new CustomGridViewLayout(gridLayout, itemSizingStrategy);
else
_layout = base.SelectLayout();
return _layout;
}
}
public class CustomGridViewLayout : GridViewLayout
{
public CustomGridViewLayout(GridItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy) : base(itemsLayout, itemSizingStrategy)
{
}
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
var attributes = base.LayoutAttributesForElementsInRect(rect);
var sections = attributes
.Where(a => string.IsNullOrEmpty(a.RepresentedElementKind))
.GroupBy(a => a.IndexPath.Section);
foreach (var section in sections)
{
if(section.Count() == 1)
{
var attribute = section.First();
var frame = attribute.Frame;
frame.X = 0;
attribute.Frame = frame;
}
}
return attributes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment