Swift viewWillTransition未调用 [英] Swift viewWillTransition not called

查看:642
本文介绍了Swift viewWillTransition未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UICollectionView 创建全屏图片库。当用户旋转设备时,我对

I'm creating a full screen image gallery using a UICollectionView. When the user rotates the device, I perform updates to the UICollectionView within

func viewWillTransition中的 UICollectionView 进行更新(大小:CGSize,协调器:UIViewControllerTransitionCoordinator)

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

我展示此 UIViewController 并以 UICollectionView 占据全屏。在 viewDidLoad 中,我将流程布局创建为:

I present this UIViewController modally and have a UICollectionView taking up the full screen. Within viewDidLoad, I create the flow layout as:

let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
photosCollectionView.isPagingEnabled = true
photosCollectionView.setCollectionViewLayout(flowLayout, animated: true)

我的尺寸也为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return photosCollectionView.frame.size
}

当我旋转我的设备, viewWillTransition(大小:CGSize,带有协调器:UIViewControllerTransitionCoordinator)永远不会被调用,这会导致 UICollectionViewLayout 不能调用更新。旋转设备时,确实收到消息:

When I rotate my device, viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) is never called, which causes the UICollectionViewLayout to not update. While I rotate the device, I do get the message:

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.

我已经在线阅读可以添加的内容:

I've read online that I can add:

self.automaticallyAdjustsScrollViewInsets = false

code> UIViewController ,但这没有影响。 UICollectionView 没有内容或节插图。

to the UIViewController, but that had no affect. There are no content or section insets with the UICollectionView.

在函数中我也调用了 super.viewWillTransition 。谁能帮助我解决可能导致此问题的原因?

I also have the super.viewWillTransition called within the function as well. Can anyone assist me on what could be causing this issue?

推荐答案

如果您只是担心设备旋转时的布局,那么请使用:

If you are just concern about the layout when the device rotate then please use:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    collectionView.collectionViewLayout.invalidateLayout()
    collectionView.layoutIfNeeded()
}

从苹果文档开始:

public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)




当视图控制器的视图大小为其父级更改的视图大小为
时,将调用此方法根视图控制器的
窗口旋转或调整大小时)。
如果重写此方法,则应调用super将更改传播给子级,或手动将更改转发给
个子级。

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

I guess you might called this function on a parent of that view without calling super

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

A work around would also be to register for the device rotation:

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)
}

这篇关于Swift viewWillTransition未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆