问题
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.
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
Here is my code.
- (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;
}
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.
In the second picture pickerView is my CustomView which contains UICollectionView.
Any help will be great.
回答1:
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)
来源:https://stackoverflow.com/questions/34997364/uicollectionview-bounds-width-is-always-600