ios – 未调用Swift viewWillTransition

ios – 未调用Swift viewWillTransition,第1张

概述我正在使用UICollectionView创建一个全屏图像库.当用户旋转设备时,我会对其中的UICollectionView执行更新 func viewWillTransition(to size:CGSize,with coordinator:UIViewControllerTransitionCoordinator) 我以模态方式呈现此UIViewController并使用UICollecti 我正在使用UICollectionVIEw创建一个全屏图像库.当用户旋转设备时,我会对其中的UICollectionVIEw执行更新

func vIEwWillTransition(to size:CGSize,with coordinator:UIVIEwControllerTransitionCoordinator)

我以模态方式呈现此UIVIEwController并使用UICollectionVIEw占据整个屏幕.在vIEwDIDLoad中,我创建流布局为:

let flowLayout = UICollectionVIEwFlowLayout()flowLayout.scrollDirection = .horizontalflowLayout.minimumInteritemSpacing = 0flowLayout.minimumlinespacing = 0photosCollectionVIEw.isPagingEnabled = truephotosCollectionVIEw.setCollectionVIEwLayout(flowLayout,animated: true)

我的大小也是:

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {    return photosCollectionVIEw.frame.size}

当我旋转我的设备时,从不调用vIEwWillTransition(大小:CGSize,带协调器:UIVIEwControllerTransitionCoordinator),这会导致UICollectionVIEwLayout不更新.当我旋转设备时,我收到消息:

The behavior of the UICollectionVIEwFlowLayout is not defined because: the item height must be less than the height of the UICollectionVIEw minus the section insets top and bottom values,minus the content insets top and bottom values.

我在网上看到我可以添加:

self.automaticallyAdjustsScrollVIEwInsets = false

到UIVIEwController,但没有任何影响. UICollectionVIEw没有内容或部分插入.

我也在函数内调用了super.vIEwWillTransition.任何人都可以帮助我解决可能导致此问题的原因吗?

解决方法 如果您只关心设备旋转时的布局,请使用:

overrIDe func vIEwDIDLayoutSubvIEws() {    super.vIEwDIDLayoutSubvIEws()    collectionVIEw.collectionVIEwLayout.invalIDateLayout()    collectionVIEw.layoutIfNeeded()}

来自apple docs:

public func vIEwWillTransition(to size: CGSize,with coordinator: UIVIEwControllerTransitionCoordinator)

This method is called when the vIEw controller’s vIEw’s size is
changed by its parent (i.e. for the root vIEw controller when its
window rotates or is resized).
If you overrIDe this method,you should either call super to propagate the change to children or manually forward the change to
children.

我猜你可以在不调用super的情况下在该视图的父节点上调用此函数

解决方法也是注册设备轮换:

overrIDe func vIEwWillAppear(_ animated: Bool) {    super.vIEwWillAppear(animated)    NotificationCenter.default.addobserver(self,selector: #selector(deviceOrIEntationDIDChange),name: NSNotification.name.UIDeviceOrIEntationDIDChange,object: nil)}overrIDe func vIEwWilldisappear(_ animated: Bool) {    super.vIEwWilldisappear(animated)    NotificationCenter.default.removeObserver(self)}func deviceOrIEntationDIDChange(_ notification: Notification) {    let orIEntation = UIDevice.current.orIEntation    print(orIEntation)}
总结

以上是内存溢出为你收集整理的ios – 未调用Swift viewWillTransition全部内容,希望文章能够帮你解决ios – 未调用Swift viewWillTransition所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1009660.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存