UICollectionView 无法更改单元格中 imageView 的大小

3
我想将原型单元格和imageView的宽度和高度配置为新值,但这对imageView没有用。
如何更改imageView的大小以适应单元格大小?
我从头开始创建了一个CollectionView。在IB中,我向原型单元格添加了带有标记100的ImageView。
单元格大小通过“collectionView:layout:sizeForItemAtIndexPath:”在代码中改变,这个方法很好用。然后,我配置了imageView的框架来适应单元格的框架。日志显示这有效果,但在模拟器中imageView的大小没有改变!
IB和模拟器中的结果:https://istack.dev59.com/3d3ES.webp 日志:
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 0, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 1, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 2, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 3, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 4, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 5, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 6, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 7, width: 104.000000
[collectionView:layout:sizeForItemAtIndexPath:] [Line 78] SETTING SIZE For Cell 8, width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 52] Entering Cell: 0
[collectionView:cellForItemAtIndexPath:] [Line 60]      width: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 61]      height: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 64]      new width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 67]      imageView width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 68]      cell width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 52] Entering Cell: 1
[collectionView:cellForItemAtIndexPath:] [Line 60]      width: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 61]      height: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 64]      new width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 67]      imageView width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 68]      cell width: 104.000000
[... and so on ...]
[collectionView:cellForItemAtIndexPath:] [Line 52] Entering Cell: 8
[collectionView:cellForItemAtIndexPath:] [Line 60]      width: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 61]      height: 50.000000
[collectionView:cellForItemAtIndexPath:] [Line 64]      new width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 67]      imageView width: 104.000000
[collectionView:cellForItemAtIndexPath:] [Line 68]      cell width: 104.000000

CollectionViewController.h

#import <UIKit/UIKit.h>

@interface CollectionViewController : UICollectionViewController

@end

CollectionViewController.m

#import "CollectionViewController.h"

@interface CollectionViewController ()

@end

@implementation CollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.collectionView.backgroundColor = [UIColor purpleColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Collection view
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    DLog(@"Entering Cell: %ld", (long)indexPath.item);

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];

    CGRect frame = [imageView frame];
    DLog(@"\t\twidth: %f", frame.size.width);
    DLog(@"\t\theight: %f", frame.size.height);

    frame.size.width = 104;
    DLog(@"\t\tnew width: %f", frame.size.width);

    [imageView setFrame:frame];
    DLog(@"\t\timageView width: %f", imageView.frame.size.width);
    DLog(@"\t\tcell width: %f", cell.frame.size.width);

    imageView.image = [UIImage imageNamed:@"foo.jpg"];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGSize mElementSize = CGSizeMake(104, 104);
    DLog(@"SETTING SIZE For Cell %d, width: %f", indexPath.row, mElementSize.width);
    return mElementSize;
}

@end

4
你正在使用自动布局。如果启用了它,就无法修改.frame或.bounds属性。 - Eugene
做到了!完美,谢谢!我在IB中更改了单元格的自动布局。 - jerik
1个回答

0
希望这能帮助到某些人。我遇到了类似的问题,但最终解决了它。如果你在- (void)awakeFromNib函数中创建自定义的UICollectionViewCell,那么可以在cell的UIImageView上调用autoresizingMask方法。
_imageViewForCell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

这应该可以调整您集合视图单元格中的图像大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接