本文介绍了在iOS 13上未调用`systemLayoutSizeFittingSize`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS 13 Beta上的 systemLayoutSizeFittingSize 遇到问题.我正在为NativeScript开发一个插件,该插件使用 UICollectionViewCell systemLayoutSizeFittingSize 来测量和布局"单元格中的UIView.我一直在做的是:

I have an issue with systemLayoutSizeFittingSize on iOS 13 beta. I am working on a plugin for NativeScript which uses the systemLayoutSizeFittingSize of the UICollectionViewCell to "measure and layout" the UIView that is in the cell. What I have been doing is:

  • 如果我需要强制对单元格中的 UIView 进行度量/布局,请手动调用 systemLayoutSizeFittingSize
  • Manually call systemLayoutSizeFittingSize if I need to force a measure/layout of the UIView that is in the cell

  • 等待操作系统调用 systemLayoutSizeFittingSize 并再次测量和布局单元格中的 UIView
  • Wait for the OS to call systemLayoutSizeFittingSize and again measure and layout the UIView that is in the cell

所有功能都可以完美运行,直到iOS 13 beta为止,OS本身调用 systemLayoutSizeFittingSize API的方式似乎已经发生了变化.在iOS 13 beta中,API根本没有被操作系统调用,而是使用 collectionView:layout:sizeForItemAtIndexPath: API来确定UICollectionViewCell的大小.在iOS 12及更低版本中,UICollectionView完成其初始布局传递后,它将调用每个UICollectionViewCell的 systemLayoutSizeFittingSize ,这使您有机会让单元格知道其大小.在iOS 13中,不再调用 systemLayoutSizeFittingSize .

It all worked perfectly until iOS 13 beta where it looks like there has been a change in the way the systemLayoutSizeFittingSize API is being called by the OS itself. In iOS 13 beta that API is not called by the OS at all and is rather using the collectionView:layout:sizeForItemAtIndexPath: API to determine the size of a UICollectionViewCell. In iOS 12 and lower after the UICollectionView finishes its initial layout pass it calls each UICollectionViewCell's systemLayoutSizeFittingSize which allows you to give a chance for the cell to tells its size. In iOS 13 systemLayoutSizeFittingSize is no longer called.

也许我必须更改或调用UIView本身或UICollectionView才能使其像在iOS 12中一样工作,但是如果我使用 systemLayoutSizeFittingSize 正确.

Maybe it is something that I have to change or call on the UIView itself or the UICollectionView to make it work as it used to in iOS 12 but I am a bit lost in understanding if I am using the systemLayoutSizeFittingSize correctly.

所以我的问题是,我是否应该期望操作系统自动调用 systemLayoutSizeFittingSize ,或者这只是手动强制 UICollectionViewCell 告知其大小的一种方式?总体而言,我是否正确使用了它?

So my question is, Should I expect the systemLayoutSizeFittingSize to be called by the OS automatically or this is simply a way to manually force a UICollectionViewCell to tell its size? And overall am I using it correctly?

经过进一步调查,看来iOS 13(测试版)上的 systemLayoutSizeFittingSize 有所更改.设置布局的 estimatedItemSize 时,它不再像以前那样被调用.我创建了一个项目,该项目显示了由行为更改引起的问题,并将向iOS团队报告.可以在此处找到该项目.此问题导致以下行为:由于不再调用 systemLayoutSizeFittingSize ,因此在使用UICollectionView时无法实现自定义大小"单元格.

After further investigation, it looks like there is a change in the systemLayoutSizeFittingSize on iOS 13 (beta). It is no longer being called as it used to be when the estimatedItemSize of the layout is set. I created a project that shows the issue caused by this change in behavior and I will be reporting this to the iOS team. The project can be found here. This issue leads to the behavior where you cannot implement "Self-sizing" cells when working with UICollectionView as the systemLayoutSizeFittingSize is no longer being called.

推荐答案

当前iOS 13.0 beta版( simulator版本11.0(SimulatorApp-895.6 SimulatorKit-553.12 CoreSimulator-643.11 )(发行说明中未记录).更改是,在 UICollectionViewCell systemLayoutSizeFittingSize 不再被调用,而> systemLayoutSizeFitting(_:withHorizo​​ntalFittingPriority:verticalFittingPriority:).

It looks like there has been an internal breaking change with current iOS 13.0 beta (simulator Version 11.0 (SimulatorApp-895.6 SimulatorKit-553.12 CoreSimulator-643.11) that is not documented in the release notes. The change is that systemLayoutSizeFittingSize of an UICollectionViewCell is no longer called while the systemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:) is called.

所以:

    在iOS上
  • systemLayoutSizeFittingSize 在在iOS> 13上调用
  • systemLayoutSizeFitting(_:withHorizo​​ntalFittingPriority:verticalFittingPriority:)

因此,作为解决方案/解决方法,您必须复制 systemLayoutSizeFittingSize 中的代码,并将其放在 systemLayoutSizeFitting(_:withHorizo​​ntalFittingPriority:verticalFittingPriority:)中.虽然这不是一个完美的解决方案/解决方法,但它至少为我们提供了一个可行的解决方案,但很有可能在iOS 13脱离Beta版并发布且不再需要之前可以对此进行更改.代码示例:

So as a solution/workaround you have to duplicate your code from systemLayoutSizeFittingSize and place it inside systemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:). While this is not perfect solution/workaround it at least gives us the a working solution for now, it is very much possible that this can change before iOS 13 is out of beta version and is released and is no longer required. Code example:

import UIKit

class FlickPhotoCell: UICollectionViewCell {

    func getCellSize(_ targetSize: CGSize) -> CGSize {
        return CGSize(width: 50, height: 200)
    }

    // Only this is called on iOS 12 and lower
    override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize {
        return self.getCellSize(targetSize)
    }

    // Only this is called on iOS 13 beta
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        return self.getCellSize(targetSize)
    }
}

这篇关于在iOS 13上未调用`systemLayoutSizeFittingSize`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:15