So I have a working collection view with 2 columns and 3 items in each column. The grid view works great until I get to the iPhone 5s simulator, and then my grid view turns into one single column instead of 2. I've read a bunch of posts about custom layouts but none of them seem to work. Any help is appreciated. Here is my code:
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: 8, left: 8, bottom: 8, right: 8)
layout.itemSize = CGSize(width: (screenWidth - 32)/2, height: 175)
var collview = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight), collectionViewLayout: layout)
[collview.reloadData];
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
let myImage = UIImage(named: pictureArray[indexPath.row])
cell.textView.text = textArray[indexPath.row]
cell.imageView.image = myImage
return cell
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictureArray.count
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: 175, height: 175)
}
Because you set the size for each item is CGSize(width: 175, height: 175)
, so when you change other phone, it will not work well.
Just change like this: (change UICollectionViewDelegate
to UICollectionViewDelegateFlowLayout
)
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: screenWidth / 3, height: screenWidth / 3)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.