繁体   English   中英

uicollectionview旋转单元可见性及位置和顺序

[英]uicollectionview rotation cell visibility & position and order

我有一个UICollectionView应该具有3列和5行。 它们是静态的,不会更改。 UICollectionView不应滚动,因此单元UICollectionView可见。 在纵向模式下看起来不错,但是在横向模式下视图变得混乱。 如何以有意义的方式对细胞进行重新排序? 还是可以拉伸内容以使所有单元格都可见?

肖像 在此处输入图片说明

风景 在此处输入图片说明

- (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
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM