UICollectionView 边界宽度始终为 600
UICollectionView Bounds Width is always 600
我有一个自定义 UIView
说 CustomView
,它有一个 UICollectionView
作为它的子视图。我已将此 customView 添加到 UITableView Prototype Cell 中,并将所有四个(顶部、底部、左侧、右侧)边缘固定到 TableViewCell
.contentView
.
的标准距离
现在我想设置 UICollectionView
部分昆虫。在这种方法中 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
当我得到 UICollectionView.bounds.width
时,我总是得到 600
无论我是 运行 它是 4s、5s、6 或 6 plus 还是 iPad.这很烦人,我无法为 UICollectionView
部分昆虫设置正确的 insect
。任何帮助都会很棒。谢谢
这是我的代码。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
NSInteger number = [self collectionView:collectionView numberOfItemsInSection:section];
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
CGSize firstSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:number - 1 inSection:section];
CGSize lastSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:lastIndexPath];
UIEdgeInsets insect = UIEdgeInsetsMake(0, (collectionView.bounds.size.width - firstSize.width) / 2,
0, (collectionView.bounds.size.width - lastSize.width) / 2);
return insect;
}
这是我的 CustomView 的初始化,它有 collectionView 作为子视图
- (void) initilize {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc ] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout: flowLayout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.collectionView registerClass:[PickerCollectionViewCell class] forCellWithReuseIdentifier:@"PickerCollectionViewCell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.allowsMultipleSelection = YES;
[self addSubview:self.collectionView ];
self.backgroundColor = [UIColor clearColor];
}
附件中还有我的故事板的两张图片。
在第二张图片中,pickerView 是我的 CustomView,其中包含 UICollectionView
。
任何帮助都会很棒。
宽度等于 600,因为您在 UIView 更新当前屏幕的约束之前使用它。您可以在 - (void)viewDidLayoutSubviews
方法中检查 UICollectionView 框架,您将收到实际值。
这里有两个选项:
1) 您可以使昆虫计算独立于 UICollectionView 框架
要么
2) 您可以 re-calculate 在 viewDidLayoutSubviews
触发后重新绘制 UICollectionView
集合视图布局
(我宁愿第一个)
我有一个自定义 UIView
说 CustomView
,它有一个 UICollectionView
作为它的子视图。我已将此 customView 添加到 UITableView Prototype Cell 中,并将所有四个(顶部、底部、左侧、右侧)边缘固定到 TableViewCell
.contentView
.
现在我想设置 UICollectionView
部分昆虫。在这种方法中 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
当我得到 UICollectionView.bounds.width
时,我总是得到 600
无论我是 运行 它是 4s、5s、6 或 6 plus 还是 iPad.这很烦人,我无法为 UICollectionView
部分昆虫设置正确的 insect
。任何帮助都会很棒。谢谢
这是我的代码。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
NSInteger number = [self collectionView:collectionView numberOfItemsInSection:section];
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
CGSize firstSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:number - 1 inSection:section];
CGSize lastSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:lastIndexPath];
UIEdgeInsets insect = UIEdgeInsetsMake(0, (collectionView.bounds.size.width - firstSize.width) / 2,
0, (collectionView.bounds.size.width - lastSize.width) / 2);
return insect;
}
这是我的 CustomView 的初始化,它有 collectionView 作为子视图
- (void) initilize {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc ] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout: flowLayout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.collectionView registerClass:[PickerCollectionViewCell class] forCellWithReuseIdentifier:@"PickerCollectionViewCell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.allowsMultipleSelection = YES;
[self addSubview:self.collectionView ];
self.backgroundColor = [UIColor clearColor];
}
附件中还有我的故事板的两张图片。
在第二张图片中,pickerView 是我的 CustomView,其中包含 UICollectionView
。
任何帮助都会很棒。
宽度等于 600,因为您在 UIView 更新当前屏幕的约束之前使用它。您可以在 - (void)viewDidLayoutSubviews
方法中检查 UICollectionView 框架,您将收到实际值。
这里有两个选项:
1) 您可以使昆虫计算独立于 UICollectionView 框架
要么
2) 您可以 re-calculate 在 viewDidLayoutSubviews
触发后重新绘制 UICollectionView
集合视图布局
(我宁愿第一个)