IOS TOPICS

IOS Introduction
Swift UI Label
Swift UI Button
Swift UI Text Field
Swift UI Slider
Swift UI Switch
Swift UI Stepper
Swift UI Date Picker
Swift UI Segmented Control
Views
Table View
Collection Views
SCROLL VIEW
iOS Content View
iOS View Controllers
iOS Bar interface
iOS Navigation interface
iOS Architect Pattern
iOS Libraries
iOS Web request and parsing
iOS user defaults
iOS Coredata and database

Make Private Collection View


Make it private and use the following piece of code

import UIKit class CollectionViewCell: UICollectionViewCell { @IBOutlet private weak var LabelName: UILabel! func configure( with LabelName : String){ LabelName.text = dataSource } }

Going back to the CollectionViewController and implement cell for item at index path to use of the custom collection cells.

import UIKit class CollectionViewController: UICollectionViewController { let dataSource: [String] = ["Aanya", "Akhil" , "Ryan", "Suzie", "Lakshya", "Vivant", "Tejaswi", "Zayn"] override func viewDidLoad() { super.viewDidLoad() } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataSource.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell = UICollectionViewCell() if let LabelName = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? CollectionViewCell{ LabelName.configure(with: dataSource[indexPath.row]) cell = LabelName } return cell } }

Now go back to the main storyboard and set the Custom View Controller Scene to “Initial View Controller in the Attributed inspector.

ios output

Next set the Custom Class to CollectionViewController from the generic class under the identity Inpector

ios output

Further, give it a storyboard ID and tick the checkbox to “Use Storyboard ID”

ios output

Finally, select the collection view cell on the left and go to the attribute inspector. Give the identifier the name “Cell”

ios output

Now, we can add customizations to the cells using the main Storyboard using the size inspector to alter the size of each cell in the grid. We can also center align the label with constraints

ios output ios output

You can choose scroll direction which we leave at vertical

ios output

And change cell colour from content view

ios output

To increase the number of cells and even out their sizes, go to Collection View, set number of items and Collection View Flow Layout and set Estimate size to None. This is what you’ll see

ios output