Tôi phải tạo UICollectionViewCell tùy chỉnh để hiển thị hình ảnh và tên cho hình ảnh, Và ở phía trên cùng của hình ảnh, có hình ảnh khung lớn hơn 2 pixel chiều rộng và chiều cao so với hình ảnh. Tuy nhiên, dù tôi có làm gì đi chăng nữa thì hình ảnh vẫn lớn hơn hình ảnh khung. Tôi đã làm điều tương tự cho xem bảng, và nó hoạt động hoàn hảo.UIImageView hiển thị hình ảnh ngoài giới hạn trong lớp phụ UICollectionViewCell
Đây là mã:
//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;
}
Các UICollectionViewCell có kích thước 67 x 100, vì vậy trong các mã, các bgImage nghĩa vụ phải được luôn luôn là 67 x 67 và nguồn gốc của nó là (0,0) và contentImage phải có khung (0,0,63,63). Bằng cách gỡ lỗi, có vẻ như chính xác. Tuy nhiên, conentimage luôn lớn hơn bgImage. Kích thước ban đầu của hình ảnh là 80 x 80. Tôi đã thử setClipToBounds, setContentViewMode trên cell.ContentView hoặc imageView, nhưng không có công trình nào.
Ảnh chụp màn hình về sự cố được đính kèm.
Mọi trợ giúp đều được đánh giá cao.
Tôi cũng đã cố gắng bố trí tất cả các phần tử trong một ngòi và trong bảng phân cảnh, nhận được kết quả tương tự. – Zhao