UICollectionView无法显示所有内容

时间:2018-01-22 13:12:15

标签: ios objective-c uicollectionview

我使用它初始化集合视图(代码不是故事板):

-(id) init{
    self = [super init];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.page = 0;
    self.step = 20;
    self.subCateItems = [[NSMutableArray alloc] init];
    self.scaleFactor = [[UIScreen mainScreen] bounds].size.width / 375.f;
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(9.f*self.scaleFactor, 6.f*self.scaleFactor, 0, 6.f*self.scaleFactor);
    _collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self updateData];
    }];

    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_collectionView];

    return self;
}

但是集合视图似乎无法显示所有集合单元格:

enter image description here

如何解决这个问题?

正如评论所暗示的那样,我已经制定了视图的视图层次结构

enter image description here

2 个答案:

答案 0 :(得分:1)

从滚动视图指示器判断,滚动视图的底部插图设置不正确,因为它是UITabBar的底层。代码在Swift中,但你应该得到这个想法; - )

我通常这样做是为了让我的滚动视图显示在顶部:

_collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, -50, 0) 
_collectionView.contentInset = UIEdgeInsetsMake(0, 0, -50, 0)

另外,请务必这样做:

self.automaticallyAdjustsScrollViewInsets = false 

答案 1 :(得分:0)

第一个问题在这里

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

必须像这样

    self.view = [[UIView alloc] initWithFrame:self.view.bounds];

然后,谁是self?它是UITabBarController的标签吗? 如果是的话,一切都很好。如果不是,那么您将设置UICollectionView

的正确插入