3

I am using a UICollectionrView to show videos. Everything works fine except that I get a warning saying :

2015-10-30 14:00:39.893 test[6451:90574] the behavior of the UICollectionViewFlowLayout is not defined because:

2015-10-30 14:00:39.893 test[6451:90574] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

2015-10-30 14:00:39.893 test[6451:90574] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {320, 0}> collection view layout: .

2015-10-30 14:00:39.894 test[6451:90574] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

This warning appears just when I reload the data inside my collectionView.

I have tried to change width but the warning stills there.

What am I doing wrong?

var UserVideosInfo = [[String]]()

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "reloadCollectionVideoView:",
            name: "ReloadCollectionVideoView",
            object: nil)   
    }

@objc func reloadCollectionVideoView(notification: NSNotification) {
        UserVideosInfo = NSUserDefaults.standardUserDefaults().objectForKey("UserVideosJSON") as! [[String]]
        print(UserVideosInfo)
        collectionView?.reloadData()

    }

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if UserVideosInfo.count == 0 {
            return 0
        }else{
        return UserVideosInfo.count
        }

    }



    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as UICollectionViewCell
        let communityViewController = storyboard?.instantiateViewControllerWithIdentifier("community_id")

        videoCell.frame.size.width = (communityViewController?.view.frame.size.width)!
        videoCell.center.x = (communityViewController?.view.center.x)!

        return videoCell
    }
SNos
  • 3,280
  • 4
  • 34
  • 79

4 Answers4

4

Looks like one of your elements exceeds the size of your UICollectionView after you reload your data. I've had this warning happen on rotation from landscape to portrait, where my landscape cells would obviously not fit inside a portrait UICollectionView.

Try invalidating the current layout before reloading your data:

self.collectionView.collectionViewLayout.invalidateLayout()

If that doesn't work, try setting the width of your cells to a specific size (for example 300) and work from there. You may find that there are margins or edges that take space that you weren't expecting, and that's why your elements are larger than the width of your UICollectionView. If that is the case, try setting the self.collectionView.contentInset to UIEdgeInsetsZero

Erken
  • 1,418
  • 14
  • 20
  • I had this trouble but only on iPhone 5S/iPhone SE and invalidating the layout worked for me, thanks ! – Livio Oct 10 '16 at 08:43
1

I'm still researching this error, "uicollectionviewflowlayout is not defined because." And I can't figure out the solution, yet (I will get you bug!), but I did narrow down exactly what is causing my issue, the contentOffset is being set with bad values and causing the error:

contentOffset: {0, -64};

I am using AKPickerView-Swift, I converted it to Swift 3. I only get the error when I embed the UIView, containing the AKPickerView, in a Navigation Controller (UINavigationController) on the storyboard.

So the simple temporary solution and just a work around, don't embed the UIView in a Navigation Controller. :) I'll come back later and correct this properly and figure out why UICollectionViewFlowLayout is being fired off for the Navigation Controller and not the UIViewController.

If anyone has free time to figure it out before I do, Thanks in Advance and it's greatly appreciated.

SammyRNYCreal
  • 531
  • 4
  • 6
0

I just got this error after I purged all builds and rebuilt the project. I had made several other changes and prior to this everything was working "correctly", or at least it appeared to be (perhaps due to the view being cached somehow - not sure).

Anyway, I also had contentOffset: {0, -64} so I double checked the constraints and I found that my constraints were set on the superview (navigation controller), and not the view. I removed the constraints to the superview and re-added them, this time to the view. Now the contentOffset is {0, 0} and the error is gone.

Mary Martinez
  • 150
  • 3
  • 10
0
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as UICollectionViewCell 
(........)
}

Is your UICollectionView outlet name collectionview like you specified here???

If you are using a "UICollectionView" in the storyboard, then outlet name specified for that in the class should need to use here. Example:

let videoCell = myCollectionViewOutletName.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as MyCollectionViewCellClassName
iUser
  • 1,035
  • 3
  • 19
  • 49
Ram Madhavan
  • 2,166
  • 1
  • 15
  • 20