swift 集合视图大小调整问题
swift collection view resizing problems
所以我遇到的问题是,集合视图,取决于我使用的模拟器,它在一个设置中显示 3(iPhone 5) 列或 4 列(iPhone 6 ) 在另一个。我希望集合视图始终显示 4 列,无论屏幕大小。这是我目前使用的一些代码:
override func viewDidLoad() {
super.viewDidLoad()
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 100, height: 80)
collview1 = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight), collectionViewLayout: layout)
[collview1.reloadData];
}
您需要将 layout.itemSize 更改为计算值。
类似于:
layout.itemSize = CGSize(width: (screenWidth - spacing) / numberOfColumns, height: desiredHeight)
间距是一行中项目间的总间距加上左右插图。
DesiredHeight 可能应该是一个计算值,以保持设备之间单元格的纵横比。
希望对您有所帮助。
所以我遇到的问题是,集合视图,取决于我使用的模拟器,它在一个设置中显示 3(iPhone 5) 列或 4 列(iPhone 6 ) 在另一个。我希望集合视图始终显示 4 列,无论屏幕大小。这是我目前使用的一些代码:
override func viewDidLoad() {
super.viewDidLoad()
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 100, height: 80)
collview1 = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight), collectionViewLayout: layout)
[collview1.reloadData];
}
您需要将 layout.itemSize 更改为计算值。
类似于:
layout.itemSize = CGSize(width: (screenWidth - spacing) / numberOfColumns, height: desiredHeight)
间距是一行中项目间的总间距加上左右插图。
DesiredHeight 可能应该是一个计算值,以保持设备之间单元格的纵横比。
希望对您有所帮助。