I added UICollectionView by code.
Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter.
Do
Swift 3.0, Swift 4.0 and Swift 5.0
It's work for me.
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
In Method (inside viewDidLoad)
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
}