UICollectionView 边界宽度始终为 600 [英] UICollectionView Bounds Width is always 600

查看:23
本文介绍了UICollectionView 边界宽度始终为 600的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 UIViewCustomView 它有一个 UICollectionView 作为它的子视图.我在 UITableView Prototype Cell 中添加了这个 customView,并使用 TableViewCell.contentView 将所有四个(顶部、底部、左侧、右侧)边缘固定到标准距离.

I have a custom UIView say CustomView which has a UICollectionView as its SubView. I have added this customView inside a UITableView Prototype Cell and pined all four (top, bottom, left, right) edges to the standard distance with TableViewCell.contentView.

现在我想设置 UICollectionView 部分昆虫.在这个方法 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 中,当我得到 UICollectionView.bounds.width 时,无论我是在 4s、5s、6 或 6 plus 还是 iPad 上运行它,我总是得到 600.这很烦人,我无法为 UICollectionView 部分昆虫设置正确的 insect.任何帮助都会很棒.谢谢

Now i want to set the UICollectionView section insect. In this method - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section when i get UICollectionView.bounds.width, i always get 600 no matter if i am running it on 4s, 5s, 6 or 6 plus or iPad. This is very annoying and i can't set the proper insect for the UICollectionView section insect. Any help will be great. thanks

这是我的代码.

- (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 作为子视图

here is my initlize of my CustomView which has collectionView as a subView

- (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];
}

还附上了我的故事板的两张图片.

Attached are also two images for my story board.

在第二张图片pickerView 中是我的CustomView,其中包含UICollectionView.

In the second picture pickerView is my CustomView which contains UICollectionView.

任何帮助都会很棒.

推荐答案

Width 等于 600 因为你在 UIView 更新当前屏幕的约束之前使用它.您可以在 - (void)viewDidLayoutSubviews 方法中检查 UICollectionView 框架,您将收到实际值.
这里有两个选项:
1) 您可以独立于 UICollectionView 框架进行昆虫计算
或者
2) 您可以在 viewDidLayoutSubviews 触发并重绘 UICollectionView
后重新计算集合视图布局(我宁愿第一个)

Width equals 600 because you take it before UIView updates constraint for current screen. You can check UICollectionView frame in - (void)viewDidLayoutSubviews method and you will receive actual value.
Here is two options:
1) You can make insects calculation independent from UICollectionView frame
or
2) You can re-calculate collection view layout after when viewDidLayoutSubviews fires and redraw UICollectionView
(I would rather the first one)

这篇关于UICollectionView 边界宽度始终为 600的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆