我必須創建一個自定義的UICollectionViewCell來顯示圖像和圖像的名稱,並且在圖像頂部有一個幀圖像,其大小爲2像素寬度和高度比圖像。但是,無論我做什麼,圖像似乎都比幀圖像大。我爲桌面視圖做了同樣的事情,並且它非常完美。UIImageView在UICollectionViewCell子類中顯示圖像越界
下面是代碼:
//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×100,所以在代碼中,bgImage應該總是爲67 X 67和它的起源是(0,0 )和contentImage應該有一個(0,0,63,63)的框架。通過調試,這似乎是正確的。但是,conentimage總是比bgImage大。儘管圖像的原始大小是80 x 80。我試過setContentViewMode在cell.ContentView或imageView上setClipToBounds, ,但沒有任何作品。
附上有關該問題的屏幕截圖。
任何幫助表示讚賞。
我也嘗試在一個筆尖和故事板中佈局所有元素,得到相同的結果。 – Zhao