gpt4 book ai didi

ios - 如何在 viewWillTransitionToSize 之后获取 subview 的大小

转载 作者:搜寻专家 更新时间:2023-10-31 22:34:33 27 4
gpt4 key购买 nike

我有一个 UICollectionView,它由弹出窗口显示。 UICollectionView 包含 16 行,每行 10 个单元格,我希望这些单元格填满 UICollectionView 的宽度。我正在尝试调整 viewWillTransitionToSize 中每个单元格的大小,但我无法获得正确的值。

如何获取 UICollectionView 的宽度,因为它将在 过渡后显示?

在 iPad Air 2 上切换 50/50 和 70/30 分屏时出现问题。

从 70/30 更改为 50/50 使我的单元格太小:

70/30 correct

50/50 incorrect

从 50/50 更改为 70/30 使我的单元格太大:

50/50 correct

70/30 incorrect

当前代码:

class ColorPickerCollectionViewController: UICollectionViewController {


var cellsize:CGSize?;


override func viewDidLoad() {
super.viewDidLoad()

// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

// Do any additional setup after loading the view.
if let flowLayout = self.collectionViewLayout as? UICollectionViewFlowLayout {

flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
flowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0);

}

}

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 16;
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10;
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell

// Configure the cell

let colorView = getColorViewForCell(indexPath, cell: cell);
cell.contentView.addSubview(colorView);

cell.selectedBackgroundView = UIView();
cell.selectedBackgroundView!.backgroundColor = UIColor.whiteColor();

return cell
}

func getColorViewForCell(indexPath:NSIndexPath, cell:UICollectionViewCell) -> UIView {
let colorView = UIView();
colorView.backgroundColor = UIColor.blueColor();
colorView.frame = CGRectMake(cell.bounds.origin.x + 1, cell.bounds.origin.y + 1, cell.bounds.width - 2, cell.bounds.height - 2);
colorView.translatesAutoresizingMaskIntoConstraints = false;
return colorView;
}


func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

if self.cellsize == nil {
let width = CGFloat(collectionView.frame.width / 10);
cellsize = CGSize(width: width, height: width);
}

return self.cellsize!;
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

NSLog("viewWillTransitionToSize")
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator);

//What do I do here?
self.cellsize //= ???;


let cView:UICollectionView = self.collectionView!;
cView.performBatchUpdates(nil, completion: nil);


}

}

最佳答案

新的解决方案,感谢@MikePollard 关于子类化 UICollectionViewFlowLayout 的评论。

我创建了一个子类 ColorPickerFlowLayout,其完整内容如下。我将其设置为 Collection View 的布局,并让 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 返回布局的单元格大小。

然后我可以删除 UICollectionViewController 中的 viewWillTransitionToSize

import UIKit

class ColorPickerFlowLayout: UICollectionViewFlowLayout {

private var cellSize:CGSize? = nil
var previousBounds: CGRect? = nil

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func getCellSize() -> CGSize? {

return cellSize
}

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {

if previousBounds == nil || previousBounds!.width != newBounds.width {

let width = CGFloat(newBounds.width / 10)
cellSize = CGSize(width: width, height: width)

previousBounds = newBounds

self.collectionView?.performBatchUpdates(nil, completion: nil)

return true
} else {

return false
}

}

}

关于ios - 如何在 viewWillTransitionToSize 之后获取 subview 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32933675/

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