Skip to content

alexpaul/Programmatic-UI-Xibs-Storyboards

Repository files navigation

Programmatic-UI-Xibs-Storyboards

Mixing programmatic UI code with storyboards and xibs.

xibs storyboards code

Registering a custom cell in code

// register collection view cell
podcastView.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "podcastCell")

Registering a xib file for creating a custom cell in code

// register collection view cell using xib/nib
podcastView.collectionView.register(UINib(nibName: "PodcastCell", bundle: nil), forCellWithReuseIdentifier: "podcastCell")

Setting up a collectin view in code

NB: Reminder, a colleciton view must have a layout.

// collection view
public lazy var collectionView: UICollectionView = {
  let layout = UICollectionViewFlowLayout()
  layout.scrollDirection = .vertical
  layout.itemSize = CGSize(width: 400, height: 400)
  let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
  cv.backgroundColor = .yellow
  return cv
}()

Getting a view controller instance from storyboard

guard let podcastDetailController = podcastDetailStoryboard.instantiateViewController(identifier: "PodcastDetailController") as? PodcastDetailController else {
  fatalError("could not downcast to PodcastDetailController")
}

Segue via push segue to a view controller

navigationController?.pushViewController(podcastDetailController, animated: true)

Setting the item size of a collection view cell via the UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  // override the default values of the itemSize layout from the collectionView property initializer in the PodcastView
  let maxSize: CGSize = UIScreen.main.bounds.size
  let itemWidth: CGFloat = maxSize.width * 0.95 // 95% of the width of device
  return CGSize(width: itemWidth, height: 120)
}
Configuring large title on the navigation bar
navigationController?.navigationBar.prefersLargeTitles = true

About

Mixiing programmatic UI code with storyboards and nib/xibs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages