LaVOZs

The World’s Largest Online Community for Developers

'; ios - Swift 2 - the behavior of the UICollectionViewFlowLayout is not defined because: - LavOzs.Com

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
    }

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

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.

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.

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
Related
performSelector may cause a leak because its selector is unknown
the behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width
UIAlertController : UICollectionViewFlowLayout is not defined warning every time I try to bringup a UIAlertcontroller
Adding UICollectionViewController to UIViewController Not Working
Can't implement required methods of JSQMessagesViewController Swift 3
Why is UICollectionView insetForSectionsAtIndex not being called?
Swift Error - Use of undeclared type 'cell' - Collection View
Swift vertical UICollectionView inside UITableView
Multiple UICollection View on single UIView Scrolling is not working while cell data loading from API