Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UICollectionViewCell size programmatically in Swift?

I have a UICollectionView with a segmented control to switch between data. But how do I change the UICollectionViewCell size properties programmatically ? then I can custom each style appropriately for each data type.

For example change the width to 520 points and height to 100. I am new to iOS development and am experienced with CSS :/

I know how to do it in Xcode (obviously) but do not know the correct methods to change it in swift. Any ideas or feedback is much appreciated (Y)

enter image description here

like image 715
RileyDev Avatar asked Jul 27 '15 19:07

RileyDev


People also ask

How do I increase the collection height of a cell in Swift?

Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from storyboard. Show activity on this post. You can put the width and height you want in the CGSize constructor.

How do I resize a collection view cell?

First you have to Add the UICollectionViewDelegateFlowLayout delegate and then use following delegate method it will work fine for me. Show activity on this post. Implement the below method,, it is autoresized base on the screen you have.


1 Answers

Make sure you conform to UICollectionViewDelegateFlowLayout for your class (for swift4)

eg. class MyClass: UICollectionViewDelegateFlowLayout 

Prior to Swift 3

//Use for size func collectionView(collectionView: UICollectionView,         layout collectionViewLayout: UICollectionViewLayout,         sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {      } //Use for interspacing     func collectionView(collectionView: UICollectionView,         layout collectionViewLayout: UICollectionViewLayout,         minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {             return 1.0     }      func collectionView(collectionView: UICollectionView, layout         collectionViewLayout: UICollectionViewLayout,         minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {             return 1.0     } 

// For swift 3

func collectionView(_ collectionView: UICollectionView,                     layout collectionViewLayout: UICollectionViewLayout,                     sizeForItemAt indexPath: IndexPath) -> CGSize {  }  func collectionView(_ collectionView: UICollectionView,                     layout collectionViewLayout: UICollectionViewLayout,                     minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {     return 1.0 }  func collectionView(_ collectionView: UICollectionView, layout     collectionViewLayout: UICollectionViewLayout,                     minimumLineSpacingForSectionAt section: Int) -> CGFloat {     return 1.0 } 

// For swift 4

extension MyClass: UICollectionViewDelegateFlowLayout {          func collectionView(_ collectionView: UICollectionView,                             layout collectionViewLayout: UICollectionViewLayout,                             sizeForItemAt indexPath: IndexPath) -> CGSize {          }          func collectionView(_ collectionView: UICollectionView,                             layout collectionViewLayout: UICollectionViewLayout,                             minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {             return 1.0         }          func collectionView(_ collectionView: UICollectionView, layout             collectionViewLayout: UICollectionViewLayout,                             minimumLineSpacingForSectionAt section: Int) -> CGFloat {             return 1.0         }     } 

use the same code as shown above for swift 3 but just make sure your class conforms to protocol UICollectionViewDelegateFlowLayout

like image 52
Sumit Oberoi Avatar answered Sep 24 '22 17:09

Sumit Oberoi