我有一个UICollection视图,看起来像这样(见两个紫色和蓝色边框):
在iOS 10上没有标题/空白,但在iOS11上有。我已经尝试了这里提到的所有内容:How can I enable/disable section headers in UICollectionView programmatically?
我的代码目前看起来像这样:
- (void) viewDidLoad
{
[super viewDidLoad];
autocompleteLayout = [[SQLProAutocompleteFlowLayout alloc] init];
autocompleteLayout.headerReferenceSize = CGSizeZero;
autocompleteCollectionView = [[UICollectionView alloc] initWithFrame: CGRectZero
collectionViewLayout: autocompleteLayout];
autocompleteCollectionView.insetsLayoutMarginsFromSafeArea = NO;
[self.view addSubview: autocompleteCollectionView];
// removed layout code (leftAnchor, rightAnchor, topAnchor, bottomAnchor).
autocompleteCollectionView.delegate = self;
autocompleteCollectionView.dataSource = self;
// Removed nib register code
self.view.layer.borderColor = [UIColor redColor].CGColor;
self.view.layer.borderWidth = 1;
autocompleteCollectionView.layer.borderColor = [UIColor blueColor].CGColor;
autocompleteCollectionView.layer.borderWidth = 2;
}
- (CGSize) collectionView: (UICollectionView *) collectionView
layout: (UICollectionViewLayout *) collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeZero;
}
我的FlowLayout看起来像这样:
- (void) setupLayout
{
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
} // End of setupLayout
- (void) prepareLayout
{
[super setItemSize: [self itemSize]];
[super setSectionInset: [self sectionInset]];
self.minimumLineSpacing = 0;
[super prepareLayout];
} // End of prepareLayout
- (CGFloat) itemWidth
{
return self.collectionView.frame.size.width;
}
- (CGSize) itemSize
{
CGSize result = CGSizeMake(self.collectionView.frame.size.width,
rowHeight);
return result;
}
- (void) setItemSize:(CGSize)itemSize
{
self.itemSize = CGSizeMake(self.collectionView.frame.size.width,
rowHeight);
}
- (CGPoint) targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
{
return CGPointZero;
}
我还验证了UICollectionView的contentInset为零。
我错过了什么?为什么我的UICollectionView仍然有空格?
答案 0 :(得分:14)
在iOS 11中,对UIScrollView进行了一些更改。
尝试设置contentInsetAdjustmentBehavior属性
<table>
<tr><td>
<table bgcolor=#334433>
<tr><td>
Al
</td></tr>
</table>
</td></tr>
<tr><td>
<table bgcolor=#334433>
<tr><td>
Peter Wells
</td></tr>
</table>
</td></tr>
<tr><td>
<table bgcolor=#334433>
<tr><td>
World Champions in the 20th century
</td></tr>
</table>
</td></tr>
</table>
答案 1 :(得分:1)
试试这个:
self.automaticallyAdjustsScrollViewInsets = false
- (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(-25,0,0,0); // top, left, bottom, right
}