UIcollectionView Cell的子视图调整大小以适合单元格大小

时间:2014-08-28 07:21:27

标签: ios7 uicollectionviewcell

我有一个UICollectionView,我已经将一些自定义单元格作为我的网格主菜单。我已经覆盖了sizeForItemAtIndexPath方法来调整单元格的大小,但是单元格内的图像视图仍然保持与原来相同的大小(我想在大小检查器中)。

我也尝试在cellForItemAtIndexPath()中调整imageview的大小,但什么都没发生。

MainMenuViewCell.m

-(id)initWithFrame:(CGRect)frame
{
  self=[super initWithFrame:frame];
  if (self)
  {
     self.menuImage=[[UIImageView alloc]init];

     //i have tried here to resize it but nothing was happened

     [self.contentView insertSubview:self.menuImage atIndex:0];
  }
  return self;
}

-(void)prepareForReuse
{
   [super prepareForReuse];
   [self.menuImage setImage:nil];
   [self setNeedsLayout];
}

-(void)layoutSubviews
{
   [super layoutSubviews];       
   self.contentView.bounds=CGRecctMake(0,0,self.bounds.size.width,self.bounds.size.width);
   menuImage.frame=CGRecctMake(0,0,self.bounds.size.width,self.bounds.size.width);
}

MainMenuController.m

-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPAth
{
   MainMenuViewCell *itemCell=(MainMenuViewCell *)[self.collectionView dequeueReusableCellWithIntifier:@"menuItem" forIndexPath:indexPath];
   itemCell.menuImage.image=[UIImage imageNamed:@"test"];

   return itemCell;
}

-(CGSize*) collectionView:(UICollectionView *)collectionView layout:collectionViewLayout  sizeForItemAtIndexPath:(NSIndexPath *)indexPAth
{
   return screen width / 2;
}

0 个答案:

没有答案
相关问题