问题
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 ?
回答1:
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.
回答2:
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?
来源:https://stackoverflow.com/questions/26085840/getting-width-of-the-notification-center-inside-today-extension