7 Replies
      Latest reply on Oct 30, 2018 12:50 PM by Emlyn B
      cmaier Level 2 Level 2 (20 points)

        Noticed a bunch of my collection views in beta 3 are showing up too tall, but immediately shrink to size when I scroll.  Looks like it is setting the cell size to layout.estimatedItemSize and not resizing to fit prior to drawing (these are autosized, with full constraints).

         

        I've solved for now by making my view controllers UICollectionViewDelegateFlowLayout, and doing:

         

            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        ... create dummy cell

        ... populate cell with appropriate label text, etc., to make it the right size

        let size = cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)

                  

           return size

        }

         

        I think this is a bug in beta 3 since it worked fine in the first two betas and 11.x, but could be my constraints are weird or something.

        • Re: issue with collection view sizing
          sjs Level 1 Level 1 (10 points)

          Seeing this too in beta 4. Works fine in 11.x.

           

          * layout.estimatedItemSize is UICollectionViewFlowLayoutAutomaticSize.

           

          * Constraints completely specify the cell.

           

          The initial size on screen is the default (50x50). Scrolling/rotating/resizing causes the layout to update. Invalidating the flow layout does not.

           

          Off to see if I can create a simple reproducible case.

          • Re: issue with collection view sizing
            Mark Collette @ Connected Level 1 Level 1 (0 points)

            Has anyone filed a radar? We're seeing this as well, and would like to follow-up on the fix.

            We're sizing the cell itself, not the contentView.

              • Re: issue with collection view sizing
                jml5qh Level 1 Level 1 (0 points)

                I've been in contact with Apple about the issue but no official fix yet. There is an open radar to address this issue. We have found one workaround. First, make sure you set self.contentView.translatesAutoresizingMaskIntoConstraints = false. You will also need to override preferredLayoutAttributesFitting with the following code:

                 

                    override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
                        if #available(iOS 12.0, tvOS 12.0, *)
                        {
                            /*
                            In iOS 12, there are issues with self-sizing cells. The fix is to call updateConstraintsIfNeeded() and turn on translatesAutoResizingMaskIntoConstraints as described in Apples release notes:
                            "You might encounter issues with systemLayoutSizeFitting(_:) when using a UICollectionViewCell subclass that requires updateConstraints(). (42138227) — Workaround: Don't call the cell's setNeedsUpdateConstraints() method unless you need to support live constraint changes. If you need to support live constraint changes, call updateConstraintsIfNeeded() before calling systemLayoutSizeFitting(_:)."
                            */
                            self.updateConstraintsIfNeeded()
                        
                        
                        return super.preferredLayoutAttributesFitting(layoutAttributes)
                
                  • Re: issue with collection view sizing
                    Emlyn B Level 1 Level 1 (0 points)

                    I just ran into this issue today with my code. It is thoroughly broken in iOS 12 release on Xcode 10.

                    My situation is further complicated as I've got a custom UICollectionViewLayout - which needs the height to calculate its layout.

                     

                    The code works fine on an 11.4 simulator, but not for 12.1.

                     

                    Is there a RDAR for this already?

                • Re: issue with collection view sizing
                  HUY-VU Level 1 Level 1 (0 points)

                  Having the same issue in production now and wondering how is the status.

                  I calculate manually using intrinsicContentSize of internal components as a workaround for now.