如何将 UICollectionViewCell 高度设置为视图(屏幕)高度的 90%?
How to set UICollectionViewCell height to 90% of the View (Screen) height?
我正在尝试将 UICollectionViewCell 高度设置为屏幕尺寸的 90%。因此,当使用不同的设备时,单元格大小看起来会很好。
问题是我不确定如何以编程方式设置单元格高度。所有单元格的大小应该相同,因此无需考虑内容,我只希望单元格高度为屏幕的 90%。
这是 iphone 6 屏幕中的样子:
但是,当切换到较小的设备时,它看起来像这样:
预先感谢您的帮助!
您可以通过编程方式设置 UICollectionView
的流程布局磁贴大小。有几个不同的地方可能会导致这种情况,对于初学者,我将从 viewWillAppear
(在您的 ViewController class 中)开始:
override func viewWillAppear() {
super.viewWillAppear()
if let layout = self.schedulesView.collectionViewLayout as? UICollectionViewFlowLayout {
var cellSize = UIScreen.main.bounds.size // start with the full screen size
cellSize.height *= 0.9 // adjust the height by 90%
layout.itemSize = cellSize // set the layouts item size
}
}
可以根据device size
使用collectionViewFlowLayout
设置collectionView
itemSize
。试试下面的方法..
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
// All the values are changable according to your needs.
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 25, left: 3, bottom: 3, right: 3)
layout.minimumInteritemSpacing = 50
layout.minimumLineSpacing = 50
return CGSize(width: self.collectionView.frame.width - 50, height:self.collectionView.frame.height - 100)
}
注意: 从上面的代码中,您将获得根据设备大小覆盖视图的框类型 collectionView itemSize
。
刚刚发现问题出在 UICollectionViewCell 中的内容没有更新它们的大小。
只需要作为UICollectionViewCell的子类就可以了
override var bounds: CGRect {
didSet {
contentView.frame = bounds
}
}
我正在尝试将 UICollectionViewCell 高度设置为屏幕尺寸的 90%。因此,当使用不同的设备时,单元格大小看起来会很好。
问题是我不确定如何以编程方式设置单元格高度。所有单元格的大小应该相同,因此无需考虑内容,我只希望单元格高度为屏幕的 90%。
这是 iphone 6 屏幕中的样子:
但是,当切换到较小的设备时,它看起来像这样:
预先感谢您的帮助!
您可以通过编程方式设置 UICollectionView
的流程布局磁贴大小。有几个不同的地方可能会导致这种情况,对于初学者,我将从 viewWillAppear
(在您的 ViewController class 中)开始:
override func viewWillAppear() {
super.viewWillAppear()
if let layout = self.schedulesView.collectionViewLayout as? UICollectionViewFlowLayout {
var cellSize = UIScreen.main.bounds.size // start with the full screen size
cellSize.height *= 0.9 // adjust the height by 90%
layout.itemSize = cellSize // set the layouts item size
}
}
可以根据device size
使用collectionViewFlowLayout
设置collectionView
itemSize
。试试下面的方法..
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
// All the values are changable according to your needs.
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 25, left: 3, bottom: 3, right: 3)
layout.minimumInteritemSpacing = 50
layout.minimumLineSpacing = 50
return CGSize(width: self.collectionView.frame.width - 50, height:self.collectionView.frame.height - 100)
}
注意: 从上面的代码中,您将获得根据设备大小覆盖视图的框类型 collectionView itemSize
。
刚刚发现问题出在 UICollectionViewCell 中的内容没有更新它们的大小。
只需要作为UICollectionViewCell的子类就可以了
override var bounds: CGRect {
didSet {
contentView.frame = bounds
}
}