UIImageview does not resize in UICollectionView

I'm trying to resize a UIImageView in a UICollectionView, but only the UICollectionViewCell resizes, but not the UIImageView. I'm not sure what is going wrong. I have a UIImageview on a storyboard.

Please find my code for reference.

My View Controller

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *animButton;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(IBAction)animation:(id)sender;
@end

#import "ViewController.h"
#import "CollectionViewCell.h"
#import "SingleView.h"
#import "GridView.h"

@interface ViewController ()
@property (nonatomic, strong) SingleView *largeLayout;
@property (nonatomic, strong) GridView *smallLayout;
@property(nonatomic, strong)NSArray * imageArray;
@end
static NSString *ItemIdentifier = @"PatternCell";
@implementation ViewController

- (void)viewDidLoad {


  //  [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];

    self.collectionView.delegate= self;
    self.collectionView.dataSource=self;
    self.smallLayout = [[GridView alloc] init];
    self.largeLayout = [[SingleView alloc] init];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)animation:(id)sender{

    if (self.collectionView.collectionViewLayout == self.smallLayout)
    {

        UIImage * image = [UIImage imageNamed:@"on"];

        [self.animButton setBackgroundImage:image forState:UIControlStateNormal];
        [self.largeLayout invalidateLayout];
        [self.collectionView reloadData];
        [self.collectionView setCollectionViewLayout:self.largeLayout animated:YES];

    }
    else
    { UIImage * image = [UIImage imageNamed:@"off"];
         [self.smallLayout invalidateLayout];
        [self.animButton setBackgroundImage:image forState:UIControlStateNormal];
        [self.collectionView setCollectionViewLayout:self.smallLayout animated:YES];
    }

}
#pragma mark - UICollectionView DataSource & Delegate methods



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 11;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    [cell setImage:[UIImage imageNamed:[NSString stringWithFormat:@"groupA_%ld.jpg", (long)indexPath.item ]]];

    cell.dateLabel.text=@"11/12/2004";
    cell.titleLabel.text=@"SectionTitle";

        return cell;


}

@end

      

CollectionViewCell

#import <UIKit/UIKit.h>

@interface CollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

-(void)setImage:(UIImage *)image;

@end
#import "CollectionViewCell.h"

@interface CollectionViewCell()


@end

@implementation CollectionViewCell

- (id)initWithFrame:(CGRect)frame
{

    NSLog(@"Hello");
    if (!(self = [super initWithFrame:frame])) return nil;

  self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
   self.contentView.frame = self.bounds;
    self.contentView.translatesAutoresizingMaskIntoConstraints = YES;
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.dateLabel];
    [self.contentView addSubview:self.titleLabel];

    self.imageView.backgroundColor = [UIColor whiteColor];

    return self;
}



-(void)setImage:(UIImage *)image
{
    self.imageView.image = image;
}


@end

      

Layout1

import "SingleView.h"

@implementation SingleView
-(id)init
{
    if (!(self = [super init])) return nil;

    self.itemSize = CGSizeMake(364, 262);
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.minimumInteritemSpacing = 20.0f;
    self.minimumLineSpacing = 20.0f;

    return self;
}
@end

      

Layout2

#import "GridView.h"

@implementation GridView
-(id)init
{
    if (!(self = [super init])) return nil;

    self.itemSize = CGSizeMake(130, 130);
    self.sectionInset = UIEdgeInsetsMake(10, 40, 10, 40);
    self.minimumInteritemSpacing = 10.0f;
    self.minimumLineSpacing = 10.0f;

    return self;
}
@end

      

I think its my cell is not spy on the image, I don't know how to do it. or is there some other approach. Please share your thoughts.

+3


source to share





All Articles