{"id":12468,"date":"2014-07-16T13:33:26","date_gmt":"2014-07-16T17:33:26","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=12468"},"modified":"2014-07-16T13:33:26","modified_gmt":"2014-07-16T17:33:26","slug":"new-collection-view-features-in-ios-8","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/new-collection-view-features-in-ios-8\/","title":{"rendered":"New Collection View Features in iOS 8"},"content":{"rendered":"<p>\t\t\t\tCollection Views are a powerful user interface technology for laying out collections of data. With fantastic support for high performance grid layouts, flexible line based layouts, and fully custom layouts, along with integrated animation capabilities, they make creating beautiful data-driven UIs a wonderful experience both for the developer and the end user.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/collection-views.png\" alt=\"collection views\" width=\"664\" height=\"296\" class=\"aligncenter size-full wp-image-12505\" \/><\/p>\n<p>iOS 8 adds some nice enhancements to Collection Views, including the ability to self-size cells without the need for using a delegate. Likewise, the layout attributes of individual cells can now be adjusted directly from within the cell class without having to use a delegate. This makes it easier to make small layout changes that are related to a particular cell.<\/p>\n<h2>Self-sizing Cells<\/h2>\n<p>First, let&#8217;s look at how to self-size cells. Before iOS 8, creating cells that sized to their content required working within a layout class directly or via a layout&#8217;s delegate, such as the <strong>UICollectionViewDelegateFlowLayout<\/strong>.<\/p>\n<p>For example, the following code adjusts the cell size to fit cells comprised of simple UILabels, presented using a <strong>UICollectionViewFlowLayout<\/strong>:<\/p>\n<pre class=\"lang:csharp decode:true\">\nclass FlowLayoutDelegate : UICollectionViewDelegateFlowLayout\n{\n  string[] items;\n  UIStringAttributes attr;\n\n  public FlowLayoutDelegate (string[] items)\n  {\n    this.items = items;\n\n    attr = new UIStringAttributes {\n      Font = new UILabel ().Font\n    };\n  }\n\n  public override SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)\n  {\n    string text = items [indexPath.Row];\n\n    return new NSString (text).GetSizeUsingAttributes (attr);\n  }\n}\n<\/pre>\n<p>This code results in a Collection View where each cell fits the text it contains:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/self-sizing.png\" alt=\"self sizing cells\" width=\"378\" height=\"533\" class=\"aligncenter size-full wp-image-12500\" \/><\/p>\n<p>Creating cells that are self-sized is much easier in iOS 8. Simply set the <strong>EstimatedItemSize<\/strong> on the <strong>UICollectionViewFlowLayout<\/strong> and implement <strong>SizeThatFits<\/strong> or use AutoLayout in the cell class.<\/p>\n<p>For example, to self-size the cells in the example above, set <strong>EstimatedItemSize<\/strong> as shown below:<\/p>\n<pre class=\"lang:csharp decode:true\">\nflowLayout = new UICollectionViewFlowLayout (){\n  EstimatedItemSize = new SizeF (44, 144)\n};\n<\/pre>\n<p>Then, simply adding the following implementation of <strong>SizeThatFits<\/strong> in the cell class produces the same result, without the need for the <strong>UICollectionViewDelegateFlowLayout<\/strong> implementation:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override SizeF SizeThatFits (SizeF size)\n{\n  label.Frame = new RectangleF (new PointF (0, 0), label.AttributedText.Size);\n  return label.AttributedText.Size;\n}\n<\/pre>\n<h2>Adjusting Layout Attributes in Cells<\/h2>\n<p>iOS 8 also makes it easier to manipulate the layout attributes returned from within the cell class without resorting to a <strong>UICollectionViewDelegateFlowLayout<\/strong>. Just override <strong>PreferredLayoutAttributesFittingAttributes<\/strong> in the cell class.<\/p>\n<p>The following code changes the font of every other label and adjusts the layout for each cell appropriately:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittingAttributes (UICollectionViewLayoutAttributes layoutAttributes)\n{\n  var newLayoutAttributes = (UICollectionViewLayoutAttributes)layoutAttributes.Copy ();\n\n  if (layoutAttributes.IndexPath.Row % 2 == 0) {\n    \/\/\n    label.TextColor = UIColor.Red;\n    ContentView.BackgroundColor = UIColor.LightGray;\n    var attr = new NSAttributedString (Text, UIFont.SystemFontOfSize (28.0f));\n    label.Font = UIFont.SystemFontOfSize (28.0f);\n    \/\/           \n    newLayoutAttributes.Frame = new RectangleF (new PointF (0, 0), attr.Size);\n    label.Frame = new RectangleF (new PointF (0, 0), attr.Size);\n  } else {\n    \/\/\n    newLayoutAttributes.Frame = new RectangleF (new PointF (0, 0), label.AttributedText.Size);\n    label.Frame = new RectangleF (new PointF (0, 0), label.AttributedText.Size);\n  }\n\n  return newLayoutAttributes;\n}\n<\/pre>\n<p>This allows fine-grained control at the cell level, as shown below:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/layout-attributes.png\" alt=\"layout attributes\" width=\"378\" height=\"533\" class=\"aligncenter size-full wp-image-12502\" \/><\/p>\n<p>As you can see, iOS 8 adds flexibility to Collection Views, allowing granular control of cells directly from the cell class and making it easier to control cell layout and sizing when needed.<\/p>\n<p>The code from this post is available <a href=\"https:\/\/github.com\/mikebluestein\/SelfSizingCells\" title=\"SelfSizingCells GitHub repo\" target=\"_blank\">here<\/a>.<\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/20519\" title=\"Discuss in Forums\" target=\"_blank\"><em>Discuss this blog post in the Xamarin Forums<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Collection Views are a powerful user interface technology for laying out collections of data. With fantastic support for high performance grid layouts, flexible line based layouts, and fully custom layouts, along with integrated animation capabilities, they make creating beautiful data-driven UIs a wonderful experience both for the developer and the end user. iOS 8 adds [&hellip;]<\/p>\n","protected":false},"author":1932,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-12468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Collection Views are a powerful user interface technology for laying out collections of data. With fantastic support for high performance grid layouts, flexible line based layouts, and fully custom layouts, along with integrated animation capabilities, they make creating beautiful data-driven UIs a wonderful experience both for the developer and the end user. iOS 8 adds [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1932"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=12468"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/12468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=12468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=12468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=12468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}