Layout Issues UICollectionViewCell iOS9

Since moving to the new iOS9 and Xcode 7, I came across a problem with one of the UICollectionView in my application.

Apparently, the UICollectionView does not seem to update the layout and restrictions of the UICollectionViewCell correctly, only until it is reused.

Pictures speak better than words - this is what it looks like when the UIViewController first displayed: enter image description here

However, this is not the correct layout, and easy enough, when I scroll the UICollectionView horizontally to the left, I get the correct layout of the newly appeared cells: enter image description here

When I lean back, old cells that were wrong are now reused and look good.

Now, as before the transition to iOS9 and Xcode 7, my desired effect is that the cells have the correct format even when they first appear.

For your convenience, here is more detailed information about setting UICollectionView and limitations in XIB: enter image description here

In code, this is pretty standard:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell : MatchmakersCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! MatchmakersCollectionViewCell cell.imageView.backgroundColor = UIColor.lightGrayColor() cell.bottomName.text = "StackOverflow" return cell } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.matchmakers.count } func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } 

And every time I update the data source (self.matchmakers), I called self.collectionView.reloadData()

The last observation I noticed was very strange: when debugging with Xcode, the debug UICollectionViewCell hierarchy UICollectionViewCell never displayed subviews correctly and simply gave me the default UIView instead: enter image description here

+6
source share
8 answers

To solve this problem:

  • Create your custom UICollectionViewCell using Nib files (.xib) .

  • In your custom UICollectionViewCell override the didMoveToSuperView () method to add this

    self.setNeedLayout ()

    self.layoutIfNeeded ()

3.In your UIViewController method viewDidLoad ()

self.collectionView.registerNib (UINIB (nibName: "yourNibName", bundle: nil), forCellWithReuseIdentifier: "Cell")

-------- update 20150923

only need step 2, override theMoveToSuperView method to add

self.setNeedLayout ()

self.layoutIfNeeded ()

in your custom class UICollectionViewCell.

Thanks @macayer

+9
source

I did not need to create my own XIB for my cell, if I have a custom cell and associate it with my storyboard, I just added this line to (MyCustomCollectionViewCell.m):

 - (void)didMoveToSuperview{ [super didMoveToSuperview]; [self setNeedsLayout]; [self layoutIfNeeded]; } 
+4
source

I had the same error in iOS9. You may have found that everything is in order after rebooting the collection. So my solution is: set a timer for iOS9 to reload the cells.

 if #available(iOS 9, *) { self.refreshTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("refreshDataTimer"), userInfo: nil, repeats: true) } 

And reload them:

 func refreshDataTimer(){ self.collectionView?.reloadData() } 

Although this solution is a little stupid, but it works on several lines.

or you can reload each cell:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { .... self.collectionView?.reloadItemsAtIndexPaths([indexPath]) return cell } 

But it is inefficient.

+2
source

I had the same error in iOS9. I would like the center x and center y to be displayed in the UICollectionView cell. It does not work, but now I added Align Top and Align Leading to the storyboard, then I added the NSLayoutConstraint ports to

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath . I am changing NSLayoutConstraint depending on the resolution.

+1
source

I am pretty sure this is a iOS9 bug. Instead of setting the UICollectionViewCell user interface and the restrictions in the UICollectionView in the storyboard, I created its own XIB. Then added to the UIViewController :

 self.collectionView.registerNib(UINib(nibName: "MatchmakersCollectionViewCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "CollectionCell") 

Now it works well.

0
source

I had one part of the problem described in OP ...

The last thing I noticed was very strange when debugging with the Xcode hierarchy of debug views, UICollectionViewCell never correctly presented subviews and just gave me the default UIView in their place

I spent years smashing my head against the wall, trying to work out what was, none of the proposed answers changed the situation. In the end, I removed all the restrictions in my XIB cells and re-added them, and this fixed the problem. I assume that at some point the restrictions should have gone bad.

Hope this can help someone.

0
source

In my case, disabling the size classes in the .xib file did the trick.

0
source

Here is the appropriate Swift 2.0 code needed to solve this problem in Xcode 7. Note that the correct code is "setNeedsLayout" and not "SetNeedLayout", as described above. Be sure to call it in the UICollectionViewCell file.

  override func didMoveToSuperview() { self.setNeedsLayout() self.layoutIfNeeded() } 
-1
source

All Articles