objective-c – UIImageView在UICollectionViewCell子类中显示超出范围的图像

前端之家收集整理的这篇文章主要介绍了objective-c – UIImageView在UICollectionViewCell子类中显示超出范围的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须创建一个自定义UICollectionViewCell来显示图像的图像和名称.在图像的顶部,有一个帧图像,其宽度和高度比图像大2个像素.但是,无论我做什么,图像似乎都比帧图像大.我为表格视图做了同样的事情,它完美无缺.

这是代码

  1. //GridCell.h
  2.  
  3. @interface GridCell : UICollectionViewCell
  4. @property(nonatomic,strong) UILabel *lblName;
  5. @property(nonatomic,strong) UIImageView *image;
  6. @end
  7.  
  8. //GridCell.m
  9.  
  10. #import "GridCell.h"
  11.  
  12. @implementation GridCell
  13.  
  14. @synthesize image,lblName;
  15.  
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // Initialization code
  21.  
  22. UIImage *bg = [UIImage imageNamed:@"borderUIimgLg.png"];
  23.  
  24. UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,frame.size.width,frame.size.width)];
  25. [bgImage setImage:bg];
  26. [bgImage setContentMode:UIViewContentModeScaleAspectFill];
  27. NSLog(@"BG Image size %f,%f",bgImage.frame.size.width,bgImage.frame.size.height);
  28.  
  29.  
  30. UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0,2.0,frame.size.width-4.0,frame.size.width-4.0)];
  31. [contentImage setContentMode:UIViewContentModeScaleAspectFill];
  32. [contentImage setClipsToBounds:YES];
  33. self.image = contentImage;
  34.  
  35. [self.contentView addSubview:self.image];
  36.  
  37. [self.contentView addSubview:bgImage];
  38.  
  39. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2.0,frame.size.width - 4.0,21.0)];
  40. [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:11.0]];
  41. [label setTextAlignment:NSTextAlignmentCenter];
  42. [label setBackgroundColor:[UIColor clearColor]];
  43. self.lblName = label;
  44. [self.contentView addSubview:self.lblName];
  45. }
  46. return self;
  47. }

UICollectionViewCell的大小为67 x 100,因此在代码中,bgImage应始终为67 x 67,其原点为(0,0),而contentImage应为(0,63),63).通过调试,似乎是正确的.但是,conentimage总是比bgImage大.但是图像的原始尺寸为80 x 80.我试过setClipToBounds,
在cell.ContentView或imageView上使用setContentviewmode,但都不起作用.

附上有关该问题的屏幕截图.

任何帮助表示赞赏.

解决方法

你在另一个上使用2次frame.size.width而不是frame.size.height

编辑,试试这个:

在单元格中,使用initWithFrame方法时,请使用单元格的self.bounds属性.如果在边框内初始化imageview,则使用CGRectInset方法初始化具有较小边界的imageview,并将imageviews center设置为与单元格的contentview相同

猜你在找的C&C++相关文章