gpt4 book ai didi

ios - 为什么在设备旋转的纵向模式下不专门调用 UICollectionViewDelegateFlowLayout 方法?

转载 作者:行者123 更新时间:2023-11-30 11:32:46 25 4
gpt4 key购买 nike

我在 UIView 中有一个 Collection View 。我想在设备旋转时自动调整单元格大小(即,纵向模式下有 4 列,横向模式下有 5 列)。第一次(最初启动应用程序时)工作正常,但是当我将设备从横向模式再次旋转到纵向模式时,FlowLayout 委托(delegate)方法不会被调用。

这是我的代码:

    class GridViewNw: UIView, UICollectionViewDelegate {

var gridCollectionView: UICollectionView!
var imgArrCount: Int = 20

var gridLeading: NSLayoutConstraint?
var gridTrailing: NSLayoutConstraint?
var gridTop: NSLayoutConstraint?
var gridBottom: NSLayoutConstraint?
var gridWidth: NSLayoutConstraint?
var gridLeft: NSLayoutConstraint?
var gridRight: NSLayoutConstraint?
var gridCenter: NSLayoutConstraint?

override init(frame: CGRect) {
super.init(frame: frame)


let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
gridCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
gridCollectionView.dataSource = self
gridCollectionView.delegate = self
gridCollectionView.register(GridViewCell.self, forCellWithReuseIdentifier: "gridViewCell")
gridCollectionView.backgroundColor = UIColor.yellow
gridCollectionView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(gridCollectionView)


gridTop = gridCollectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0)
gridTop?.isActive = true

gridCenter = gridCollectionView.centerXAnchor.constraint(equalTo: self.centerXAnchor)
gridCenter?.isActive = true


gridLeft = gridCollectionView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0)
gridLeft?.isActive = true
gridRight = gridCollectionView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0)
gridRight?.isActive = true


gridBottom = gridCollectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0)
gridBottom?.isActive = true

gridCollectionView!.collectionViewLayout = layout

//NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

//layout.prepare()
}


required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


func getImgSize() -> (width: CGFloat, height: CGFloat, itemGap: CGFloat, collVwWidth: CGFloat){

var imgWidth: CGFloat = 115
var imgHeight: CGFloat = 173

let viewWidth: CGFloat = self.bounds.width
var columnCount: Int = 3
if UIDevice.current.userInterfaceIdiom == .pad {

imgWidth = 157
imgHeight = 236

columnCount = 4
let interfaceOrientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation
if interfaceOrientation == .landscapeLeft || interfaceOrientation == .landscapeRight {
columnCount = 5
}
}

let gapCount: Int = columnCount - 1 //CGFloat(columnCount * 2) //CGFloat(columnCount - 1)
let minimumGap: CGFloat = 10
var totalGap: CGFloat = minimumGap * CGFloat(gapCount)
var collectionViewWidth: CGFloat = viewWidth - totalGap

let remainder = Int(collectionViewWidth) % columnCount
totalGap = totalGap + CGFloat(remainder)
let itemGap = totalGap / CGFloat(gapCount)
collectionViewWidth = collectionViewWidth - CGFloat(remainder)
let itemWidth: CGFloat = (collectionViewWidth - totalGap) / CGFloat(columnCount) //(collectionViewWidth / CGFloat(columnCount)) - (minimumGap * 2)
let itemHeight: CGFloat = (imgHeight / imgWidth) * itemWidth

return (itemWidth, itemHeight, itemGap, collectionViewWidth)
}

}
extension GridViewNw: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imgArrCount
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageCell : GridViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridViewCell", for: indexPath) as! GridViewCell
//imageCell.contentView.backgroundColor = UIColor.blue

let img = getImgSize()
imageCell.displayAssetImage(imgWidth: img.width, imgHeight: img.height)

imageCell.layer.shouldRasterize = true
imageCell.layer.rasterizationScale = UIScreen.main.scale

return imageCell
}


}
extension GridViewNw : UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print("sizeForItemAt")
let img = getImgSize()
return CGSize(width: img.width, height: img.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
print("insetForSectionAt")
let img = getImgSize()
return UIEdgeInsetsMake(0, img.itemGap, 0, img.itemGap)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
print("minimumInteritemSpacingForSectionAt")
let img = getImgSize()
return img.itemGap
}
}

最佳答案

检查 viewwilltransitionviewDidLayoutSubviews 并根据需要更新您的 itemsize。了解更多信息 viewwilltransitionviewDidLayoutSubviews显示链接。

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

在这里,如果发生 View 转换,我会调用一个方法名称 updateCV ,并在该方法中根据需要更新 itemsize

func updateCV(with size: CGSize) {
if let layout = cv.collectionViewLayout as? UICollectionViewFlowLayout
{
// add your custom layout here
layout.itemSize = (size.width < size.height) ? itemSizeForPortraitMode : itemSizeForLandscapeMode
layout.invalidateLayout()
}
}

关于ios - 为什么在设备旋转的纵向模式下不专门调用 UICollectionViewDelegateFlowLayout 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50086145/

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