Last active
June 23, 2022 17:39
-
-
Save rotorgames/67a83ec9bb7438aa81e7c4d905454fdb to your computer and use it in GitHub Desktop.
CollectionView GridViewLayout one element alignment issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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