Question

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
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top