Sebastian Boldt
Sebastian Boldt

Reputation: 5321

Getting width of the Notification-Center inside Today-Extension

I am currently trying to put a CollectionView inside a Today Extension. But there is some thing that bothers me.

I want to achieve that every cell fits inside one row of my collection-view. So calculate the cells width, depending on the items count.

Everything works fine on the iPhone but on the iPad it just doesn't look right. The cells width are way to big. So i debugged my code and it seems that self.view.frame.width or self.view.bounds.width returns the full width of the Screen and not the notification-centers width. No wonder why my cells are to big. I am calculating my item-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 my question now is, how can i get the "real" width of my notification center on the ipad ?

Upvotes: 3

Views: 986

Answers (2)

Saren Inden
Saren Inden

Reputation: 3660

Are you sure that the collectionview is also resized properly? You are not taking the self.view.bounds.size.width but that of the collectionview.

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

Upvotes: 0

Malte
Malte

Reputation: 704

Just guessing here, but from my experience with UICollectionView i'd say you will need to calculate your cell size's in viewDidLayoutSubviews.

When your delegate method gets called (probably shortly after viewDidLoad:) the view might not got properly resized yet and therefore returns the wrong frame size (all viewcontrollers are initialized with the full screen size and are later resized to fit).

Doing it in viewDidLayoutSubviews should guarantee that your collectionViews Frame is already correct. You'll need to reload your collectionView though.

Upvotes: 1

Related Questions