I have a UICollectionView
in my app, that I initialize with the following code:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setPagingEnabled:YES];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"normalCell"];
[self.collectionView setShowsHorizontalScrollIndicator:NO];
[self.collectionView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:self.collectionView];
Then I added all the protocol methods:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"normalCell" forIndexPath:indexPath];
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor blueColor];
} else if (indexPath.row == 1) {
cell.backgroundColor = [UIColor greenColor];
} else if (indexPath.row == 2) {
cell.backgroundColor = [UIColor yellowColor];
}
return cell;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIEdgeInsets safeInsets = self.tabBarController.view.safeAreaInsets;
NSLog(@"Top: %f", safeInsets.top);
self.collectionView.frame = CGRectMake(0, safeInsets.top, self.view.frame.size.width, self.view.frame.size.height-safeInsets.top);
[self.collectionView reloadData];
}
Now, the UICollectionView
is placed perfectly within my view. The UICollectionViewCell
however, starts much higher than the UICollectionView
.
In the screenshot below you can see the behaviour. In blue, you can see the UICollectionViewCell
and in yellow, you can see the UICollectionView
. As you can see it seems to have a higher y-coordinate then the UICollectionView
.
I've been busting my head for a couple of hours now, but I can't seem to figure out why it's doing this. Any ideas?
在这里,您可以根据tabbarcontroller设置单元格高度,以使其高于单元格高度,而只是通过单元格设置高度。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.