【发布时间】:2012-09-26 20:04:47
【问题描述】:
我必须创建一个自定义 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。我试过 setClipToBounds, 在 cell.ContentView 或 imageView 上设置内容视图模式,但都不起作用。
附上关于该问题的截图。
感谢任何帮助。
【问题讨论】:
-
我也尝试在一个笔尖和情节提要中布局所有元素,得到相同的结果。
标签: objective-c uiimageview uiimage ios6 uicollectionview