草庐IT

ios - `systemLayoutSizeFittingSize` 未在 iOS 13 上调用

coder 2024-01-22 原文

我在 iOS 13 测试版上遇到 systemLayoutSizeFittingSize 问题。我正在为 NativeScript 开发一个插件,它使用 UICollectionViewCellsystemLayoutSizeFittingSize 来“测量和布局”单元格中的 UIView。我一直在做的是:

  • 如果我需要强制测量/布局单元格中的 UIView,请手动调用 systemLayoutSizeFittingSize

  • 等待操作系统调用 systemLayoutSizeFittingSize 并再次测量和布局单元格中的 UIView

在 iOS 13 beta 之前,这一切都运行良好,在 iOS 13 beta 中,操作系统本身调用 systemLayoutSizeFittingSize API 的方式似乎发生了变化。在 iOS 13 beta 中,操作系统根本不调用该 API,而是使用 collectionView:layout:sizeForItemAtIndexPath:用于确定 UICollectionViewCell 大小的 API。在 iOS 12 及更低版本中,在 UICollectionView 完成其初始布局传递后,它会调用每个 UICollectionViewCell 的 systemLayoutSizeFittingSize,这使您有机会让单元格告知其大小。在 iOS 13 中,不再调用 systemLayoutSizeFittingSize

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

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

编辑:

经过进一步调查,iOS 13(测试版)上的 systemLayoutSizeFittingSize 似乎发生了变化。当设置布局的 estimatedItemSize 时,它不再像以前那样被调用。我创建了一个项目来显示由这种行为变化引起的问题,我将把这个报告给 iOS 团队。该项目可以找到here .此问题导致在使用 UICollectionView 时无法实现“自动调整大小”单元格的行为,因为不再调用 systemLayoutSizeFittingSize

最佳答案

目前的 iOS 13.0 beta(模拟器版本 11.0 (SimulatorApp-895.6 SimulatorKit-553.12 CoreSimulator-643.11)似乎有一个内部重大变化,发行说明中没有记录。变化是 UICollectionViewCellsystemLayoutSizeFittingSize 不再systemLayoutSizeFitting(_:withHorizo​​ntalFittingPriority:verticalFittingPriority:) 时被调用被称为。

所以:

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

解决方案/解决方法

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

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 - `systemLayoutSizeFittingSize` 未在 iOS 13 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184502/

有关ios - `systemLayoutSizeFittingSize` 未在 iOS 13 上调用的更多相关文章

  1. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. ruby - 安装libv8(3.11.8.13)出错,Bundler无法继续 - 2

    运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 为什么在类上调用 instance_eval() 时定义类方法? - 2

    Foo=Class.newFoo.instance_evaldodefinstance_bar"instance_bar"endendputsFoo.instance_bar#=>"instance_bar"putsFoo.new.instance_bar#=>undefinedmethod‘instance_bar’我的理解是调用instance_eval在对象上应该允许您为该对象定义实例变量或方法。但是在上面的例子中,当你在类Foo上调用它来定义instance_bar方法时,instance_bar变成了一个可以用“Foo.instance_bar”调用的类方法。很明显这段代码没

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. ruby-on-rails - gem install rmagick -v 2.13.1 错误 Failed to build gem native extension on Mac OS 10.9.1 - 2

    我已经通过提供MagickWand.h的路径尝试了一切,我安装了命令工具。谁能帮帮我?$geminstallrmagick-v2.13.1Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Failedtobuildgemnativeextension./Users/ghazanfarali/.rvm/rubies/ruby-1.8.7-p357/bin/rubyextconf.rbcheckingforRubyversion>=1.8.5...yescheckingfor/

  9. ruby-on-rails - 在条件路由期间未在 Rails 3 中设置 request.subdomain - 2

    我正在尝试根据RyanBatesscreencastonsubdomains在Rails3中设置子域.但是它对我不起作用。我有以下设置:#routes.rbconstraints(Subdomain)doget'devices'=>'devices#all'end#lib/subdomain.rbclassSubdomaindefself.matches?(request)#binding.pryrequest.subdomain.present?&&request.subdomain=="admin"endend加载urladmin.localhost:3000/devices应该将

  10. ruby-on-rails - 在具有 enum_attr 的记录上调用 .all 时参数数量错误 - 2

    MODEL1有一个account_type,所以使用gem'enumerated_attributes',我制作了这样的模型:classMODEL1我不明白的奇怪的事情是,当我像这样查询任意MODEL1的种子时(这是我在ruby​​mine控制台中运行follwing命令时的错误,但在rakedb期间会发生同样的2for1错误:种子):MODEL1.all.sample和MODEL1.all我明白了:DealerLoad(0.3ms)SELECT"MODEL1".*FROM"MODEL1S"ArgumentError:wrongnumberofarguments(2for1)from/

随机推荐