私はObjective-C初心者です。 setupViewメソッドでaddSubviewを使用してイメージを表示すると、これは機能しません。addSubviewは機能しません
h. @interface CollectionViewController : UICollectionViewController @end @interface MyCell : UICollectionViewCell @end
と
m. @interface MyCell() @end @implementation MyCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) [self setupView]; return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) [self setupView]; return self; } -(UIImageView *)myImageView { UIImageView *imageView = [UIImageView new]; imageView.image = [UIImage imageNamed:@"photo.jpg"]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.masksToBounds = true; return imageView; } -(void)setupView { self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.myImageView]; } @end
画像に制約やフレームを付ける必要があります。現在は{0,0}サイズなので見えません – Shubhank