0

I'm trying to have a UICollectionViewController inside my UIViewController. Here's how I set it up

TagsCollectionViewController* tagsCollectionViewController = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:[UICollectionViewLayout new]];
    UIView *tagsView = tagsCollectionViewController.view;
    [self.view addSubview:tagsView]; // setting up constraings too

And this is my TagsCollectionViewController

@interface TagsCollectionViewController () <UICollectionViewDelegateFlowLayout>

@end

@implementation TagsCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    self.collectionView.backgroundColor = [UIColor yellowColor];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

}

#pragma mark <UICollectionViewDataSource>

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

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

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;

}

#pragma mark <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake(50, 50);
}

@end

What's strange is that I get yellow view so my collection seems to be working, however I don't get to see any items. But when I present whole controller everything works fine. So the problem occurs only when I'm trying to embed it. What can be the issue?

EDIT: Related: Adding UICollectionViewController to UIViewController Not Working

7
  • Do you keep a valid reference to your TagsCollectionViewController around so it can act as the delegate/datasource? Commented Jan 5, 2020 at 22:14
  • @PhillipMills I made it a strong property now and initialized it in viewDidLoad. Is that what you meant? Still doesn't work though. Commented Jan 5, 2020 at 22:18
  • Does its methods get called, e.g. for number of items? (Maybe put in a breakpoint to see what you're returning.) Commented Jan 5, 2020 at 22:25
  • Yes, it gets called and it works just fine as a separate controller. It probably has to do something with the way I embed it. Commented Jan 5, 2020 at 22:28
  • That's OK but I suspect cellForItemAtIndexPath is not being called at all. Commented Jan 5, 2020 at 23:12

1 Answer 1

0

It looks as if it's a problem with estimating the size of items in the collection. I was able to get results by using the following code after creating a similar example and noticing that cells weren't being requested.

@interface ViewController ()
@property (nonatomic, strong) TagsCollectionViewController *tags;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(50.0, 50.0);
    self.tags = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:layout];
    UIView *tagsView = self.tags.view;
    [self.view addSubview:tagsView];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot! I honestly not quite sure how, but it worked!!
Not sure either. Perhaps when you present the controller directly it applies some default sizing and, without that, decides not to ask for cells because it doesn't know how to display them. (Just guessing.)
It actually works even without itemSize, I'm almost sure that I tried the exact version but it didn't work back then, huh
I was initializing it with UICollectionViewLayout instead of UICollectionViewFlowLayout...
Yes, I think I gave you the right answer but with the wrong reasoning. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.