Getting the width of the Action Center inside the Today-Extension

I am currently trying to install a CollectionView inside the Today Extension. But there is something that worries me.

I want to achieve that each cell fits inside one row of my collection view. Therefore, calculate the width of the cells, depending on the number of elements.

Everything works fine on the iPhone, but it just doesn't look the same on the iPad. The width of the cells is large. So I was debugging my code and it seems like self.view.frame.width or self.view.bounds.width is returning the full width of the screen, not the width of the notification centers. No wonder my cells are big. I calculate my element size like so:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    CGFloat count = (CGFloat)[self.collectionView numberOfItemsInSection:0];
    return CGSizeMake(self.collectionView.frame.size.width/count, 80.0);;


So now my question is, how can I get the "real" width of my notification center on the ipad?


source to share

2 answers

Just guess here, but from my experience with UICollectionView I would say that you would need to calculate the size of your cell in viewDidLayoutSubviews


When your delegate method is called (possibly shortly after viewDidLoad:

), the view may not get resized properly and therefore returns the wrong frame size (all view controllers are initialized to full screen size and later resized).

Doing this in viewDidLayoutSubviews

should ensure that your CollectionViews Frame is already correct. You will need to reload your collectionView.



Are you sure the collection will also be modified correctly? You are not taking the self.view.bounds.size.width file, not the collection.

And you have the collection creation code (loadview method I assume) and your implementation of viewWillTransitionToSize / viewWillLayoutSubviews / viewDidLayoutSubviews?



All Articles