zoukankan      html  css  js  c++  java
  • Subclass UICollectionViewFlowLayout,自定义流布局

    需求:为实现第一行显示一个,第二行以后显示两个

    方案1:用系统自带的流布局,实现的效果是,若第二行只有一个,则系统默认会居中显示,不是左对齐(如下图),不符合项目要求。

    方案2:自定义系统的UICollectionViewFLowLayout,主要代码如下, 只要继承super的layoutAttributes,修改section=0,row=1的Item的X 为0即可

    (之前走了很多弯路,)

    subclass of UICollectionViewFlowLayout

    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

    {

        return YES;

    }

    - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect

    {

      NSArray<UICollectionViewLayoutAttributes*>* original = [super layoutAttributesForElementsInRect:rect];

          for (int i=original.count-1; i>=0; i--) {        

            NSIndexPath* indexPath = original[i].indexPath;

            if (nil == original[i].representedElementKind && indexPath.section==0 &&indexPath.row==1) {

                CGRect  frame = original[i].frame;

                frame.origin.x = 0;

                original[i].frame = frame;

            }

      }

        return original;

    }

  • 相关阅读:
    51nod 1416 两点 dfs
    Codeforces Round #424 (Div. 2) A-C
    Codeforces Round #423 (Div. 2) A-C
    Codeforces Round #422 (Div. 2) A-C
    HDU 6077 Time To Get Up 模拟
    51nod 1381 硬币游戏 概率
    51nod 1100 斜率最大 计算几何
    hihocoder 1287 : 数论一·Miller-Rabin质数测试 大质数判定
    字典树
    数论
  • 原文地址:https://www.cnblogs.com/Apple2U/p/5434622.html
Copyright © 2011-2022 走看看