Reputation: 2564
We have a UICollectionView
which has a prototype cell on the Storyboard. The cell has a UILabel
("label
") in it, which is positioned without autolayout.
We set the frame of the label conditionally in the -collectionView:cellForItemAtIndexPath:
with setFrame like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCell *cell;
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" forIndexPath:indexPath];
...
if (self.isViewTypeList) {
[((CategoryCell *)cell).label setFrame:FRAME_RECT];
}
...
return cell;
}
We have inspected it, and it seems that first of all it calls the label's -setFrame:
with the Storyboard values when it gets to the dequeing, then our code follows, and this sets the new frames correctly. There is no other call other than these, but the label remains at it's original position on the screen.
What's wrong? Why is the new frame not set correctly? Is something other overrdiging it? Do I have to relayout or refresh it somehow?
Upvotes: 2
Views: 1267
Reputation: 167
Uncheck Autolayout from xib . It may help to change the frame.
Upvotes: 3
Reputation: 20234
try:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//...
//instead of
//CategoryCell *cell;
//cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category"
// forIndexPath:indexPath];
CategoryCell *cell = (CategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Category"
forIndexPath:indexPath];
//sure reuse-identifier is "Category"?
//...
if (self.isViewTypeList) {
//instead of
//[((CategoryCell *)cell).label setFrame:FRAME_RECT];
[cell.label setFrame:FRAME_RECT];
}
//...
return cell;
}
also go through this link and see if you are missing some crucial steps:
http://ashfurrow.com/blog/uicollectionview-example
Upvotes: 0
Reputation: 449
Please try following steps. This will work for you-
tag
property of it in Attributes Inspector (View Section)
, like set tag = 101
After that in your code section do this -
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//...
CategoryCell *cell;
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category"
forIndexPath:indexPath];
//...
if (self.isViewTypeList) {
UILabel *lbl = (UILabel *)[cell viewWithTag:101];
[lbl setFrame:FRAME_RECT];
}
//...
}
Good Luck!
Upvotes: 2
Reputation: 2966
Autolayout will override your calls to setFrame:
.
You can add some outlets in your cell with the label's constraints, and change those to change the frame.
Upvotes: -1