0

I have a UICollectionView which should have 3 columns and 5 rows. They are static and won't be changed. the UICollectionView should not scroll, so the cells are visible the whole time. It looks good in portrait mode, but the view is messed up in landscape mode. How to reorder cells in meaningful way? or is it possible to stretch the content so that all cells are visible?

portrait: enter image description here

landscape: 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
}
Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49

1 Answers1

1

Try to find the device orientation and then swap your section and row values so that your row take place

Retro
  • 3,985
  • 2
  • 17
  • 41
  • i've implemented device orientation for number of items and sections, but it causes exception: UICollectionView recieved layout attributes for a cell with an index path that does not exist: {length = 2, path = 3 - 0}' it looks it doesn't like it – Hashmat Khalil Aug 01 '14 at 09:15
  • where do you suggest to swap those values? – Hashmat Khalil Aug 01 '14 at 09:19
  • in orientation delegate and then reload your collectionView! – Retro Aug 01 '14 at 09:26
  • swtiching and columns and rows does the trick and it useful. but in my case i needed not to switch rows & column, so I just used auto layout. thanks anyway. – Hashmat Khalil Aug 04 '14 at 13:45