uicollectionview旋转细胞能见度&位置和顺序

时间:2014-08-01 08:58:36

标签: ios uicollectionview uicollectionviewlayout

我有UICollectionView,应该有3列5行。它们是静态的,不会被改变。 UICollectionView不应滚动,因此单元格始终可见。它在纵向模式下看起来很好,但视图在横向模式下搞砸了。如何以有意义的方式重新排序细胞?或者是否可以拉伸内容以便所有细胞都可见?

肖像enter image description here

景观enter image description here

- (void)viewDidLoad
{
    [super viewDidLoad];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    UICollectionView *collec = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 160, 320, 280) collectionViewLayout:layout];
    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"test"];
    collec.backgroundColor = [UIColor yellowColor];
    collec.delegate = self;
    collec.dataSource = self;
    collec.scrollEnabled = NO;
    collec.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:collec];

    NSDictionary *headerViewDic = @{@"collec" : collec};
    NSArray *constraintsHeaderView = [NSLayoutConstraint constraintsWithVisualFormat:@"|[collec]|" options:0 metrics:nil views:headerViewDic];
    NSArray *constraintsHeaderViewV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-160-[collec]-8-|" options:0 metrics:nil views:headerViewDic];
    [self.view addConstraints:constraintsHeaderView];
    [self.view addConstraints:constraintsHeaderViewV];

    [collec setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal | UILayoutConstraintAxisVertical ];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGSize mElementSize = CGSizeMake(97, 50);
    return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,7,7,7);  // top, left, bottom, right
}

1 个答案:

答案 0 :(得分:1)

尝试找到设备方向,然后交换您的部分和行值,以便您的行发生