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

标签 ios objective-c imageview uicollectionview

我想将原型(prototype)单元格和 imageView 配置为新的宽度和高度,但这对 imageView 不起作用。

如何更改 imageView 的大小以适合单元格大小?

我从头开始新建了一个CollectionView。在 IB 上,我向带有标签 100 的原型(prototype)单元格添加了一个 ImageView

单元格大小通过 collectionView:layout:sizeForItemAtIndexPath: 在代码中更改,效果很好。然后我配置 imageView 的框架以适合单元格框架。日志显示这有效,但在模拟器上 imageView 大小没有改变!

IB和模拟器中的结果

enter image description here

日志

[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

最佳答案

我希望这对某人有所帮助。我遇到了类似的问题。我能够解决它。如果您在 - (void)awakeFromNib 函数中创建自定义 UICollectionViewCell 在单元格的 UIImageView 上调用 autoresizingMask

_imageViewForCell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

这应该会在您的 Collection View 单元格中调整图像的大小。

关于ios - UICollectionView 无法更改单元格的 imageView 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577972/

相关文章:

objective-c - 如何在我的项目中创建第二个目标来为 iOS 创建一个预填充的数据库

java - 如何从 JSON 接收到的 BASE64 字符串在 ImageView 中设置图像

Android 启动画面 ImageView 未加载

android - 有效地绘制位于 fragment 内部的 imageView 以响应触摸事件

iphone - 我正在尝试初始化要推送的 UIViewController

ios - Xcode .CalendarUnitDay 不存在

iphone - 单击 iPhone Safari 时图像消失

iOS 7 - UITableView 不保持突出显示

ios - 将参数从一个 View Controller 传递到另一个 View Controller 时崩溃

ios - UITableView : set the header frame of view in UITableView doesn't work