IOS Uicollectionlayout Customization

Source: Internet
Author: User

Objective

There are generally two types of collection view layouts:

1. Content-independent layout calculations . This is exactly what you know about things like UITableView and uicollectionviewflowlayout. The location and appearance of each cell is not based on what it displays, but the order in which all cells are displayed is based on the order of content. The default flow layout can be used as an example. Each cell is placed based on the previous cell (or, if there is not enough space, starts from the next line). Layout objects do not have to access the actual data to calculate the layout.

2. Content-based layout calculations . Our calendar view is an example of this type. In order to calculate the start and end time of the display event, the layout object needs direct access to the collection view's data source. In many cases, the layout object needs to take out not only the data of the currently visible cell, but also the data from all records that determine which cells are currently visible.

(Excerpt from an article from OBJC China)

After careful thinking, I found that custom FlowLayout didn't meet my needs, so I chose 2 to address the requirements by customizing layout.

Customize one based on the system layout, as follows

@interface Xxxlayout:uicollectionviewlayout

The main three methods in. m are as follows

A-(void) preparelayout;//  This can actually save, realize can write to the third inside B (cgsize) collectionviewcontentsize;c-(nsarray< Uicollectionviewlayoutattributes *> *) Layoutattributesforelementsinrect: (cgrect) rect;

-(BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds;// hasty words, here is the right to return yes. It's a way to tell layout to re-layout.

The first two literal understanding is his usefulness, the third is more puzzling, in fact, is a return of a Uicollectionviewlayoutattributes object array,

Uicollectionviewlayoutattributes objects more puzzling, in fact, literal translation can be known, is to tell layout objects how to layout things, of course, know the following three ways to thoroughly understand



Atindexpath: (Nsindexpath *) Indexpath;

The above three methods point in-(nsarray<uicollectionviewlayoutattributes *> *) Layoutattributesforelementsinrect: (CGRect) rect You can see it, right down here.

To date, although there is no implementation, first comb the following process. Basically is a write algorithm, b write CollectionView interval, C write cell,supplementary view implementation, and 123 is created according to requirements, create 123 of objects, you can give them some property definitions, and then save an array, let C return

The above is some logic, can not understand and nothing, the following is a simple implementation.

-(Cgsize) collectionviewcontentsize {    cgfloat contentwidth = self.collectionView.bounds.size.width;    CGFloat contentheight = self.collectionView.bounds.size.height;        Cgsize contentsize = Cgsizemake (Contentwidth, contentheight);        return contentsize; Give CollectionView a contentsize.
Self.attributearray is a nsarray attribute I wrote, Uicollectionviewlayoutattributes objects.
-(nsarray<uicollectionviewlayoutattributes *> *) Layoutattributesforelementsinrect: (CGRect) rect{NSIndexPath *indexpath = [Nsindexpath indexpathwithindex:0];
Now there is no concept of the header, because it is all custom, I just section=0 the position of a table header so write his position uicollectionviewlayoutattributes *supplementaryviewattributes =
[Self layoutattributesforsupplementaryviewofkind:uicollectionelementkindsectionheader atIndexPath:indexPath]; [Self.attributearray addobject:supplementaryviewattributes]; return Self.attributearray;} -(Uicollectionviewlayoutattributes *) Layoutattributesforsupplementaryviewofkind: (NSString *) elementKind Atindexpath: (Nsindexpath *) Indexpath {//See Clearly, this is the class method, and the 123 is not the same
Uicollectionviewlayoutattributes *attributes =
[Uicollectionviewlayoutattributes Layoutattributesforsupplementaryviewofkind:elementkind WithIndexPath:indexPath   ]; if ([Elementkind Isequaltostring:uicollectionelementkindsectionheader]) {attributes.frame = CGRectMake (0, 0, KWidt H, 50); } return attributes;}

Here, your interface should have a table head, go to CollectionView, as usual, write DataSource and delegate method.

If you want the cell, it's called 1, and I'm just going to List 2 out here as an example.

IOS Uicollectionlayout Customization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.