画像と画像の名前を表示するには、カスタマイズした UICollectionViewCell を作成する必要があります。画像の上には、画像よりも幅と高さが 2 ピクセル大きいフレーム画像があります。ただ、どう見てもフレーム画像より画像が大きいような気がします。テーブルビューにも同じことをしましたが、完璧に機能します。
コードは次のとおりです。
//GridCell.h
@interface GridCell : UICollectionViewCell
@property(nonatomic, strong) UILabel *lblName;
@property(nonatomic, strong) UIImageView *image;
@end
//GridCell.m
#import "GridCell.h"
@implementation GridCell
@synthesize image, lblName;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *bg = [UIImage imageNamed:@"borderUIimgLg.png"];
UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.width)];
[bgImage setImage:bg];
[bgImage setContentMode:UIViewContentModeScaleAspectFill];
NSLog(@"BG Image size %f, %f", bgImage.frame.size.width, bgImage.frame.size.height);
UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0, 2.0, frame.size.width-4.0, frame.size.width-4.0)];
[contentImage setContentMode:UIViewContentModeScaleAspectFill];
[contentImage setClipsToBounds:YES];
self.image = contentImage;
[self.contentView addSubview:self.image];
[self.contentView addSubview:bgImage];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2.0, frame.size.width, frame.size.width - 4.0, 21.0)];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:11.0]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
self.lblName = label;
[self.contentView addSubview:self.lblName];
}
return self;
}
UICollectionViewCell のサイズは 67 x 100 であるため、コードでは、bgImage は常に 67 x 67 であり、その原点は (0,0) であり、contentImage のフレームは (0,0,63 、63)。デバッグすると、正しいようです。ただし、conentimage は常に bgImage より大きくなります。ただし、画像の元のサイズは 80 x 80 です。cell.ContentViewまたはimageViewのいずれかでsetClipToBounds、setContentViewModeを試しましたが、どれも機能しません。
問題に関するスクリーンショットが添付されています。
どんな助けでも大歓迎です。