问题
I have a UICollectionView with a custom UICollectionViewCell. I set the alpha property in the UICollectionViewCell to 0.0f for a speific task which all works as expected. However, when I rotate the view to landscape the cell's alpha resets itself to 1.0.
I've played around with this and the collection view holds the state of selected cells and removed cells etc. but not the alpha value of the cell.
I have tried to re-set the alpha values of the changed cells on didRotateFromInterfaceOrientation:, which it does but then changes it back to 1.0 once that call has completed.
I'm trying to figure out where the alpha value of the UICollectionViewCell is resetting itself after it rotates.
If I run the below code I get the desired alpha values back for the cells, however at some point after this they all change back to 1.0:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//array containing IndexPaths for cells with 0.0f alpha value
for (NSIndexPath *path in self.selectedlettersArray) {
LetterCellView* lsv = (LetterCellView*) [self.collectionView cellForItemAtIndexPath:path];
lsv.alpha = 0.0f;
}
for (LetterCellView *lsv in self.collectionView.visibleCells) {
NSLog(@"After Cell Alpha is : %f", lsv.alpha);
}
}
Any ideas?
回答1:
In collection views the alpha - along with other properties - is not reliably managed via the usual view properties, at least this is not intended.
Instead, you should use the alpha property of UICollectionViewLayoutAttributes object that is managed by the UICollectionReusableView class via applyLayoutAttributes:.
Read up on this in the
UICollectionReusableView Class Reference and the
UICollectionViewLayoutAttributes Class Reference
回答2:
Based on Mundi's suggestion that the UICollectionViewCell alpha property is not reliable managed, I ended up adding a property to my custom UICollectionViewCell to set it to visible or hidden. Where I just set the cell's content (in my case a label) alpha to 0 or 1.
This now sticks when I rotate the device, although it seems like a bit of a workaround.
来源:https://stackoverflow.com/questions/16092463/uicollectionviewcell-alpha-value-on-rotate