Changing label position in UICollectionViewCell

2019-07-18 02:07发布

问题:

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?

回答1:

Please try following steps. This will work for you-

  • In your storyboard or XIB file select prototype cell
  • Select the label for which you want to change frame
  • Set the 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!



回答2:

Uncheck Autolayout from xib . It may help to change the frame.



回答3:

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



回答4:

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.