gpt4 book ai didi

ios - UICollectionViewCell 在设备旋转时调整大小

转载 作者:行者123 更新时间:2023-11-29 00:36:40 25 4
gpt4 key购买 nike

我有一个非常简单的 UICollectionView 嵌入在 ViewController 中。

UICollectionView 使用自动布局(在 IB 中定义)来占据 ViewController 中的整个空间。

我的 UICollectionViewCell 具有相同的大小,它们占据了 UICollectionView 的整个空间,因为我只想在 ViewController 中显示一个单元格。UICollectionViewCell 仅显示 UIImage(嵌入 UIScrollView 中以进行缩放)。

除非设备旋转,否则它工作正常。有错误消息:

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. The relevant UICollectionViewFlowLayout instance is , and it is attached to ; animations = { bounds.origin=; bounds.size=; position=; }; layer = ; contentOffset: {0, 0}; contentSize: {4875, 323}> collection view layout: .

这很清楚,我需要调整 UICollectionViewCell 的大小以适应新的高度/宽度。旋转后,UICollectionViewCell 显示适当的高度/宽度,但动画效果很差。

代码如下:

@IBOutlet weak var theCollectionView: UICollectionView!
@IBOutlet weak var theFlowLayout: UICollectionViewFlowLayout!
override func viewDidLoad() {
super.viewDidLoad()

//theCollectionView.delegate = self
theCollectionView.dataSource = self
automaticallyAdjustsScrollViewInsets = false
theFlowLayout.itemSize = theCollectionView.frame.size
theFlowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
theFlowLayout.minimumLineSpacing = 0.0
theCollectionView.reloadData()
}


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("\(#function) \(view.frame.size)")
coordinator.animate(alongsideTransition: nil, completion: {
_ in
self.theFlowLayout.itemSize = self.theCollectionView.frame.size
})
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("\(#function) \(view.frame.size)")
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("\(#function) \(view.frame.size)")
}

问题是,如何以及何时执行 UICollectionViewCell 更改以避免警告并获得流畅的动画?

当设备旋转时,我可以看到以下内容:viewWillTransition(到:with:) (375.0, 667.0)viewWillLayoutSubviews() (667.0, 375.0)viewDidLayoutSubviews() (667.0, 375.0)

该错误在 viewDidLayoutSubviews() 之后显示。我尝试在 viewDidLayoutSubviews() 中调用 theFlowLayout.invalidateLayout() 但它并没有改变问题。

我读过其他人类似的问题,但我找不到答案:-(

感谢您的帮助,

塞巴斯蒂安

最佳答案

我还没有找到最好的解决方案,但至少它有效。

我使用 viewWillTransition 来计算单元格的索引并在旋转后重置偏移量。为了避免动画效果不佳,我在动画开始时将 alpha 设置为 0,在动画结束时再次设置为 1。

viewWillLayoutSubviews 用于使 UICollectionViewFlowLayout 和 viewDidLayoutSubviews 的布局失效以设置新的单元格大小。

也许它会对其他人有帮助,但如果您有更好的解决方案,请分享!

    @IBOutlet weak var theCollectionView: UICollectionView! {
didSet {
theCollectionView.backgroundColor = UIColor.black
theCollectionView.delegate = self
theCollectionView.dataSource = self
}
}

@IBOutlet weak var theFlowLayout: UICollectionViewFlowLayout! {
didSet {
theFlowLayout.itemSize = theCollectionView.frame.size
theFlowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
theFlowLayout.minimumLineSpacing = 0.0
}
}

var assets:[PHAsset]!
var startAssetIndex = 0

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
automaticallyAdjustsScrollViewInsets = false
theCollectionView.reloadData()
}


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
theCollectionView.scrollToItem(at: IndexPath(row:startAssetIndex, section:0), at: .left, animated: true)
}


/// Resize the collectionView during device rotation
///
/// - Parameters:
/// - size: New size of the viewController
/// - coordinator: coordinator for the animation
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

// Compute the index of the image/video currently displayed
let offset = self.theCollectionView.contentOffset;
let index = round(offset.x / self.theCollectionView.bounds.size.width);

// hide the collection view to avoid horrible animation during the rotation
// animation is horrible due to the offset change
theCollectionView.alpha = 0.0
coordinator.animate(alongsideTransition: nil, completion: {
_ in
// display the collectionView during the animation
self.theCollectionView.alpha = 1.0

// compute the new offset based on the index and the new size
let newOffset = CGPoint(x: index * self.theCollectionView.frame.size.width, y: offset.y)
self.theCollectionView.setContentOffset(newOffset, animated: false)
})
}


/// Invalidate the layout of the FlowLayout, it's mandatory for the rotation
override func viewWillLayoutSubviews() {
theFlowLayout.invalidateLayout()
super.viewWillLayoutSubviews()
}


/// Set the size of the items (mandatory for the rotation)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
theFlowLayout.itemSize = theCollectionView.frame.size
}

关于ios - UICollectionViewCell 在设备旋转时调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com