2015 年 12 月 29 日更新
所以这显然适用于 UICollectionViewCell
类:
- (void)setThumbnailImage:(NSString *)thumbnailImage {
_thumbnailImage = [thumbnailImage copy];
self.thumbnailImageView.image = [UIImage imageNamed:thumbnailImage];
if (self.thumbnailImageView.layer.frame.size.width > 82.0f) {
self.thumbnailImageView.layer.cornerRadius = self.thumbnailImageView.frame.size.width/6;
}
else {
self.thumbnailImageView.layer.cornerRadius = self.thumbnailImageView.frame.size.width/2;
}
self.thumbnailImageView.clipsToBounds = YES;
self.thumbnailImageView.layer.borderColor = [UIColor whiteColor].CGColor;
self.thumbnailImageView.layer.borderWidth = 4.0f;
}
我不知道为什么 ImageView 框架在刷新后而不是在首次加载时更新。有人可以详细说明吗?
问题
我正在努力尝试获得正确的 ImageView 框架大小,以便我可以将它变成圆形而不是正方形。
在 UICollectionViewController
中,我在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
中创建一个 ImageView 圆 cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width/2;
.
问题是 ImageView 是菱形的,因为帧大小不正确,但是当我上下滚动时,刷新的单元格是圆形的,并且具有正确的帧大小,它应该在 Storyboard 中制作。
Here is the refreshed CollectionView
这是我在 UICollectionViewController
中的代码片段
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupDKScrollingView];
names = @[@"Joe Smith", @"Veronica Odel", @"Lucas Baze", @"Brian Can", @"Cory Cobler", @"Emily Jenson"];
timeRemaining = @[@"Overdue", @"0 days", @"Past", @"1 month", @"2 days", @"4 days"];
thumbnails = @[@"gray_1",@"gray_1",@"gray_1",@"gray_1",@"gray_1",@"gray_1"];
[self.collectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [names count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RZEContactsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ContactCell" forIndexPath:indexPath];
//cell.nameLabel.text = names[indexPath.row];
//cell.timeRemainingLabel.text = timeRemaining[indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:thumbnails[image.row]];
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width/2;
cell.thumbnailImageView.layer.borderWidth = 2.0f;
cell.thumbnailImageView.layer.borderColor = [UIColor blackColor].CGColor;
cell.layer.masksToBounds = YES;
cell.backgroundColor = [UIColor blueColor];
cell.layer.cornerRadius = 5.0f;
return cell;
}
最佳答案
static NSString *identifier = @"Cell";
CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.collectionImgView.clipsToBounds=YES;
cell.collectionImgView.layer.cornerRadius=cell.collectionImgView.frame.size.width/2;
cell.collectionImgView.image = [UIImage imageNamed:[_photosArr objectAtIndex:indexPath.row]];
return cell;
关于ios - 在 UICollectionViewCell 中圈出 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505855/