Boobby69
Boobby69

Reputation: 282

Size of UICollectionView is sometimes wrong

Here is my problem: I'm developing a Kodi Remote on iOS (http://forum.kodi.tv/showthread.php?tid=235973) and everything run pretty well (a classic development) but I'm having a behavior that I can't explain.

I have multiple view controllers; in one of them (a UIViewController containing a UICollectionView), I implemented this delegate method :

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize originalSize = [(UICollectionViewFlowLayout *)collectionViewLayout itemSize];
    CGSize collectionViewSize = [collectionView frame].size;

    CGFloat interItemSpacing = [(UICollectionViewFlowLayout *)collectionViewLayout minimumInteritemSpacing];

    UICollectionViewFlowLayout *collectionViewFlowLayout = (UICollectionViewFlowLayout *)[[self collectionView] collectionViewLayout];
    UIEdgeInsets sectionInset = [collectionViewFlowLayout sectionInset];

    CGFloat usableWidth = (collectionViewSize.width - ((kColumns - 1) * interItemSpacing) - sectionInset.left - sectionInset.right);
    CGFloat width =  usableWidth / kColumns;
    CGFloat height = width * originalSize.height / originalSize.width;

    return CGSizeMake(width, height);
}

I get the collection view frame (bounds is the same, of course, I don't need origin) and it works great. I get on an iPhone 5 simulator 288 width for a no matter height.

BUT, in a copy-paste of the view controller in the storyboard, I get a 304 width. The weirdest behavior is that with FLEX, and after the render, the collection view measures 288 pixels.

The "only" difference is that the first view controller is just pushed, the second is contained in a UITabBarController.

PS : the first screen I'm talking about are the 3rd & 4th screenshots.

If someone has an explication to this, I'll take it ;)

Update 1 :

If I invalidate the collection view layout in - (void)viewDidLayoutSubviews, items are correctly drawn BUT there is like a flash because the whole collection view is redrawn (the header & items below).

Upvotes: 0

Views: 2152

Answers (2)

SwiftArchitect
SwiftArchitect

Reputation: 48514

Storyboard

It makes a lot of sense when you approach the problem with a Storyboard. First, you will get a clear view of what you are designing. Second, you get to build your application with exactly 0 lines of code, leading to 0 bugs.

The parent UITabBarController and a set of UINavigationController is a common design pattern, and only when you implement it as follow do you get a proper UI behavior.

Storyboard with 1 Nav controller per tab

Code

You do not have to use a Storyboard to create the hierarchy above. You can embed each UIViewController in a UINavigationController programatically.


► Find this solution on GitHub and additional details on Swift Recipes.

Upvotes: 0

Bevan
Bevan

Reputation: 342

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      return CGSizeMake(self.collectionView.frame.size.width,
                        self.collectionView.frame.size.height);
}

and uncheck "Constrain to margins" and click on all dotted red constrain so -8 padding will be gone in iPhone 6+

enter image description here

Upvotes: 1

Related Questions