Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift:UICollectionViewCell在旋转时无法正确调整大小_Swift - Fatal编程技术网

Swift:UICollectionViewCell在旋转时无法正确调整大小

Swift:UICollectionViewCell在旋转时无法正确调整大小,swift,Swift,我有一个collectionView,并且customCollectionViewCells在所选设备上无法正确调整大小。我在viewWillTransition中处理手机大小,并为iPad和iPhone提供不同数量的手机 我迄今为止的执行情况: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt ind

我有一个
collectionView
,并且
customCollectionViewCells
在所选设备上无法正确调整大小。我在
viewWillTransition
中处理手机大小,并为iPad和iPhone提供不同数量的手机

我迄今为止的执行情况:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let safeFrame = view.safeAreaLayoutGuide.layoutFrame
    let size = CGSize(width: safeFrame.width, height: safeFrame.height)
    return setCollectionViewItemSize(size: size)

}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        layout.itemSize = setCollectionViewItemSize(size: size)
        layout.invalidateLayout()
    }
}

func setCollectionViewItemSize(size: CGSize) -> CGSize {
    let height: CGFloat = 400
    if UIDevice.current.userInterfaceIdiom == .pad {
        if UIApplication.shared.statusBarOrientation.isPortrait {
            let width = (size.width - 3 * spacing) / 2
            return CGSize(width: width, height: height)
        } else {
            let width = (size.width - 4 * spacing) / 3
            return CGSize(width: width, height: height)
        }

    } else {
        if UIApplication.shared.statusBarOrientation.isPortrait {
            let width = size.width - (2 * spacing)
            return CGSize(width: width, height: height)

        } else {
            let width = (size.width - 3 * spacing) / 2
            return CGSize(width: width, height: height)
        }
    }
}
对于大多数需要
安全区域布局指南
(即Xs、Xr)但不需要5s、6s和6s plus的iPhone,上述代码在旋转时可正确调整大小。它也不能正确调整iPad的大小

我也收到了下面的警告,但正如建议的那样,我实现了invalidateLayout(),但仍然没有消除警告并正确调整单元格大小

2019-03-13 22:33:08.679440+0800 Snapshotting a view (0x7f8f1a711650, Construction.CollectionCardCell) that has not been rendered at least once requires afterScreenUpdates:YES. 
2019-03-13 22:33:16.375665+0800 The behavior of the UICollectionViewFlowLayout is not defined because: 
2019-03-13 22:33:16.375809+0800 the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values. 
2019-03-13 22:33:16.376470+0800 The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7f8f1a40eab0>, and it is attached to <UICollectionView: 0x7f8f1a8db600; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000017de2e0>; animations = { bounds.origin=<CABasicAnimation: 0x6000019ecc20>; bounds.size=<CABasicAnimation: 0x6000019ecce0>; position=<CABasicAnimation: 0x6000019f2b80>; bounds.origin-2=<CABasicAnimation: 0x6000019f2c40>; bounds.size-2=<CABasicAnimation: 0x6000019f2c80>; }; layer = <CALayer: 0x6000019cfa60>; contentOffset: {0, -64}; contentSize: {568, 1296}; adjustedContentInset: {64, 0, 49, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7f8f1a40eab0>. 
2019-03-13 22:33:16.376574+0800 Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
2019-03-13 22:33:08.679440+0800对至少未渲染一次的视图(0x7f8f1a711650,Construction.CollectionCardCell)进行快照需要后屏幕更新:是。
2019-03-13 22:33:16.375665+0800未定义UICollectionViewFlowLayout的行为,因为:
2019-03-13 22:33:16.375809+0800项目宽度必须小于UICollectionView的宽度减去部分插入的左侧和右侧值,减去内容插入的左侧和右侧值。
2019-03-13 22:33:16.376470+0800相关的UICollectionViewFlowLayout实例为,并附加到集合视图布局:。
2019-03-13 22:33:16.376574+0800在UICollectionViewFlowLayoutBreakForInvalidSizes处创建一个符号断点,以在调试器中捕获该断点。

我创建了一个空项目,只使用基本的UICollectionViewCell测试上述代码,但不知何故,它正确地调整了自身的大小。我是否需要对customCollectionViewCells执行某些操作以强制其调整大小或重新绘制自身?

您是否找到了解决方案?当我旋转嵌入导航视图(SwiftUI)中的UICollectionView时,就会发生这种情况。我一接触到集合视图,它就修复了集合视图,并正确地呈现了它。你有没有找到解决方案?当我旋转嵌入导航视图(SwiftUI)中的UICollectionView时,就会发生这种情况。一旦我触摸到集合视图,它就会修复集合视图,并且它现在已正确渲染。