注册 登录
主题 : MyLayout 布局 UICollectionView UICollectionReusableView
级别: 新手上路
UID: 462605
精华: 0
发帖: 32
可可豆: 140 CB
威望: 140 点
在线时间: 818(时)
注册时间: 2015-04-16
最后登录: 2019-06-14
0 楼:  发表于: 2019-05-21 16:06    发自: Web Page
来源于 一般提问 分类

MyLayout 布局 UICollectionView UICollectionReusableView   

在 ipad air 2 真机调试的时候,为多个 section 设置 section headerview,uicollectionviewcell 都是自适应高度,未旋转时第二个headerview会按 estimatedItemSize 的cell布局



旋转一次后会按自动适应的高度布局



这是什么情况,我用模拟器没有问题,用 iphone 真机/模拟器也都没有问题。。。有人遇到过这种情况吗???
关键词: 问题 View ipad phone cell
级别: 新手上路
UID: 462605
精华: 0
发帖: 32
可可豆: 140 CB
威望: 140 点
在线时间: 818(时)
注册时间: 2015-04-16
最后登录: 2019-06-14
1 楼:  发表于: 2019-05-22 09:26    发自: Web Page
viewcontrol布局代码:
- (void)loadView {
    self.title = cx_location(@"CXRank");
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    rootLayout.insetsPaddingFromSafeArea = UIRectEdgeAll;
    rootLayout.backgroundColor = UIColor.whiteColor;
    self.view = rootLayout;
    
    // 排行列表
    CXRankListDelegateObject *rankListDelegate = [[CXRankListDelegateObject alloc] init];
    rankListDelegate.vc = self;
    self.rankListDelegate = rankListDelegate;
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.estimatedItemSize = CGSizeMake(10, 10);
    flowlayout.minimumInteritemSpacing = 5.0;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowlayout];
    collectionView.delegate = rankListDelegate;
    collectionView.dataSource = rankListDelegate;
    collectionView.backgroundColor = wq_colorWithHex(0xf9f9f9, 1.0);
    collectionView.alwaysBounceVertical = YES;
    collectionView.contentInset = UIEdgeInsetsMake(0, 10, 10, 10);
    collectionView.delaysContentTouches = NO;
    [collectionView registerClass:[CXRankXiaMiCCell class] forCellWithReuseIdentifier:NSStringFromClass([CXRankXiaMiCCell class])];
    [collectionView registerClass:[CXSheetCCell class] forCellWithReuseIdentifier:NSStringFromClass([CXSheetCCell class])];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CX Rank Header Resuable View"];
    
    // 添加视图
    [rootLayout addSubview:collectionView];
    
    // 布局
    collectionView.myHorzMargin = 0;
    collectionView.heightSize.equalTo(rootLayout.heightSize);
    
    self.collectionView = collectionView;
}


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

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 11;
    }else {
        return 10;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CGSize size = CGSizeMake((collectionView.wq_width - 25 - 1)/2.0, 10);
        return size;
    }else {
        int count;
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
            // 横屏显示 6 个
            count = 6;
        }else {
            // 竖屏显示 4 个
            count = 4;
        }
        CGFloat w = (collectionView.wq_width - 20 - (count - 1)*6)/count;
        return CGSizeMake(w, 10);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(collectionView.wq_width, 35);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // section header
        UICollectionReusableView *v = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CX Rank Header Resuable View" forIndexPath:indexPath];
        MyRelativeLayout *layout = [v viewWithTag:100];
        UIView *logoV = [v viewWithTag:101];
        CXLabel *titleLab = [v viewWithTag:102];
        if (!layout) {
            layout = [[MyRelativeLayout alloc] init];
            layout.tag = 100;
            layout.myMargin = 0;
            [v addSubview:layout];
        }
        if (!logoV) {
            logoV = [[UIView alloc] init];
            logoV.tag = 101;
            logoV.backgroundColor = wq_colorWithRGB(40, 166, 230, 255);
            logoV.myTop = 11;
            logoV.myHeight = 16;
            logoV.myWidth = 5;
            [layout addSubview:logoV];
        }
        if (!titleLab) {
            titleLab = [[CXLabel alloc] init];
            titleLab.tag = 102;
            titleLab.font = [UIFont systemFontOfSize:16];
            titleLab.textColor = wq_colorWithHex(0x333333, 1.0);
            titleLab.wrapContentSize = YES;
            titleLab.centerYPos.equalTo(logoV.centerYPos);
            titleLab.leftPos.equalTo(logoV.rightPos).offset(5);
            [layout addSubview:titleLab];
        }
        if (indexPath.section == 0) {
            // 虾米排行
            titleLab.key = @"CXRankXiaMi";
        }else {
            // 其它排行
            titleLab.key = @"CXRankOther";
        }
        return v;
    }else {
        return [[UICollectionReusableView alloc] init];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        // 虾米排行榜
        CXRankXiaMiCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CXRankXiaMiCCell class]) forIndexPath:indexPath];
        cell.avatarImv.image = [UIImage imageNamed:@"Placeholder"];
        cell.firstLab.text = [NSString stringWithFormat:@"第一首歌"];
        cell.secondLab.text = [NSString stringWithFormat:@"第二首歌"];
        cell.thirdLab.text = [NSString stringWithFormat:@"第三首歌"];
        return cell;
    }else {
        // 其它排行榜
        CXSheetCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CXSheetCCell class]) forIndexPath:indexPath];
        cell.titleLab.myLeft = cell.titleLab.myRight = 0;
        cell.avatarImv.image = [UIImage imageNamed:@"Placeholder"];
        cell.titleLab.text = @"稍微写长一点名称,查看长文字布局问题是否有其它问题存在其它排行榜名称";
        return cell;
    }
}

CocoaChina社区转载内容已尽可能注明出处,如未能核实来源或转发内容图片有权利瑕疵的,请及时联系社区进行修改或删除【联系方式QQ : 3442093904 邮箱:support@cocoachina.com】文章内容为作者独立观点,不代表CocoaChina社区立场。版权归原作者所有,如申请授权请联系作者,因文章侵权CocoaChina社区不承担任何法律及连带责任。

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部