Hello to all! I added the UICollectionView as a SubView to another controller, set the frame in this parent controller for the UICollectionView view, made a Scroll horizontally. I want to have only one row of items displayed on me and I could scroll horizontally, but I have 2 rows displayed (in fact, if I move up Y, then several more rows will be displayed). I tried several different methods, but they did not help me. I also tried to do it in IB using UIContainerController , it works, but I need to do it in code. How can I fix this?

My code that adds a UICollectionView

 override func viewDidLoad() { super.viewDidLoad() addUIElements() } // MARK: - UI functions private func addUIElements() { view.addSubview(headerView) let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController addChildViewController(lifelineController) lifelineController.view.frame = CGRect(x: 0, y: headerView.frame.height, width: headerView.frame.width, height: 100) view.addSubview(lifelineController.view) lifelineController.didMoveToParentViewController(self) } 

My result enter image description here

    1 answer 1

    When you create your UICollectionView , you need to tell it how you want to position the elements. This is done by passing your custom class layout to the method

     - initWithFrame:collectionViewLayout: 

    Before that, you need to make your subclass UICollectionViewLayout inherited from UICollectionViewFlowLayout , which you will transmit when initializing UICollectionView and UICollectionView in it in the intent

     - (id)init { if ((self = [super init])) { self.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.minimumLineSpacing = 10000.0f; } return self; }