我有一个collectionView
,并且customCollectionViewCells
在选定的设备上无法正确调整大小。我处理viewWillTransition
中的cellSizes,并且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)
}
}
}
对于大多数需要safeAreaLayoutGuide
(即Xs,Xr)但不需要加5s,6s和6s的iPhone而言,上面的代码在旋转时可以正确调整大小。它还无法正确调整iPad的大小。
我也收到以下警告,但是正如此post所建议的那样,我实现了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.
我创建了一个空项目,仅使用基本的UICollectionViewCell来测试上述代码,但是以某种方式正确调整了自身大小。我需要对我的customCollectionViewCells进行某些操作以强制其调整大小或重绘自身吗?