Swift4 - I can not show the CollectionView the first time “Unable to simultaneously satisfy constraints”










1















I am creating an app with Swift 4, where I make a request to the API and I want to return a result on a CollectionView.



But I get the following error, which I think is from constraints:



enter image description here



This block is repeated 100 times.



And the result is that he does not paint any cells. Showing an image like this:



enter image description here



Unless I press the top button "CHANGE AUTOLAYOUT" twice. Which is when you paint the cells of the two display modes you have, and it looks like this:



enter image description here



And this:



enter image description here



But the problem is, initially nothing is shown and should be shown. And the error that I show you in the beginning appears.



To help you a little, because I would say that the problem derives from the constrainst applied, I attach some images with the different constrainsts applied.



The initial xib, where the collectionView is, are:



enter image description here



The cell that is initially loaded is:



enter image description here



The cell once we have changed the layout is this:



enter image description here



I attached the code of the main class, the ViewVontroller that controls the CollectionView:



import UIKit
import RxSwift
import RxCocoa

final class SpeedRunListViewController: UIViewController

@IBOutlet private var collectionView: UICollectionView!
@IBOutlet private var buttonChangeLayout: UIButton!

private let disposeBag = DisposeBag()
private var viewModelList: SpeedRunListViewModel?
private var numElementsByCol: CGFloat = 3

override func viewDidLoad()
super.viewDidLoad()
navigationController?.isNavigationBarHidden = true
setupCollectionView()
viewModelList = SpeedRunListViewModel(interactor: InteractorSpeedRunSearch())
setupRx(viewModel: viewModelList!)
viewModelList?.fetch()


override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()


override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = true



private func setupCollectionView()
registerCollectionCells()
if #available(iOS 10.0, *)
collectionView.isPrefetchingEnabled = false
else
// Fallback on earlier versions

calculateLayoutCollectionItem()



private func registerCollectionCells()
collectionView.register(UINib(nibName: SpeedRunRowCollectionViewCell.nibName, bundle: nil),
forCellWithReuseIdentifier: SpeedRunRowCollectionViewCell.reuseCellId)

collectionView.register(UINib(nibName: SpeedRunCollectionViewCell.nibName, bundle: nil),
forCellWithReuseIdentifier: SpeedRunCollectionViewCell.reuseCellId)



private func calculateLayoutCollectionItem()
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout.estimatedItemSize = CGSize.init(width: 2, height: 2)



private func setupRx(viewModel: SpeedRunListViewModel)
viewModel.numElements.asObservable().subscribe(onNext: e in
self.collectionView.reloadData()
, onError: error in

, onCompleted:

, onDisposed:

).disposed(by: disposeBag)


buttonChangeLayout.rx.tap.subscribe(onNext: void in
guard let value = self.viewModelList?.layoutRow else
return

self.viewModelList?.layoutRow = !value
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()
, onError: error in

, onCompleted:

, onDisposed:

).disposed(by: disposeBag)



fileprivate func getCellId() -> String
if let layoutRow = self.viewModelList?.layoutRow, layoutRow == true
return SpeedRunRowCollectionViewCell.reuseCellId

return SpeedRunCollectionViewCell.reuseCellId




//MARK: - UICollectionViewDelegate, UICollectionViewDataSource
extension SpeedRunListViewController: UICollectionViewDelegate,
UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
guard let numElements = viewModelList?.numElements else
return 0

return numElements.value


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: getCellId(), for: indexPath) as! SpeedRunCollectionViewCellBase
if let cellViewModel = viewModelList?.getCellViewModel(index: indexPath.row)
cell.setupCell(viewModel: cellViewModel)


return cell



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
guard let speedRun = viewModelList?.getSpeedRun(index: indexPath.row) else
return


let interactorDetail: InteractorSpeedRunDetail = InteractorSpeedRunDetail(speedRun: speedRun)
let viewControllerDetail: SpeedRunDetailViewController = SpeedRunDetailViewController(interactor: interactorDetail)
viewControllerDetail.URISpeedRunDetail = (speedRun.links![1].uri)!

navigationController?.pushViewController(viewControllerDetail, animated: true)






And the truth is that I do not know why that conflict of layouts occurs. But it's driving me crazy ... I can not understand how the cells are not shown initially (because data is being received). What could it be?



Thank you very much, any question you attach it to me.



[CODE UPDATED]



These is the code solution:



//MARK: - UICollectionViewDelegateFlowLayout
extension SpeedRunListViewController: UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize


if let value = self.viewModelList?.layoutRow
if value
return CGSize(width: 320, height: 144)

else
return CGSize(width: 96, height: 162)



return CGSize(width: 320, height: 144)












share|improve this question




























    1















    I am creating an app with Swift 4, where I make a request to the API and I want to return a result on a CollectionView.



    But I get the following error, which I think is from constraints:



    enter image description here



    This block is repeated 100 times.



    And the result is that he does not paint any cells. Showing an image like this:



    enter image description here



    Unless I press the top button "CHANGE AUTOLAYOUT" twice. Which is when you paint the cells of the two display modes you have, and it looks like this:



    enter image description here



    And this:



    enter image description here



    But the problem is, initially nothing is shown and should be shown. And the error that I show you in the beginning appears.



    To help you a little, because I would say that the problem derives from the constrainst applied, I attach some images with the different constrainsts applied.



    The initial xib, where the collectionView is, are:



    enter image description here



    The cell that is initially loaded is:



    enter image description here



    The cell once we have changed the layout is this:



    enter image description here



    I attached the code of the main class, the ViewVontroller that controls the CollectionView:



    import UIKit
    import RxSwift
    import RxCocoa

    final class SpeedRunListViewController: UIViewController

    @IBOutlet private var collectionView: UICollectionView!
    @IBOutlet private var buttonChangeLayout: UIButton!

    private let disposeBag = DisposeBag()
    private var viewModelList: SpeedRunListViewModel?
    private var numElementsByCol: CGFloat = 3

    override func viewDidLoad()
    super.viewDidLoad()
    navigationController?.isNavigationBarHidden = true
    setupCollectionView()
    viewModelList = SpeedRunListViewModel(interactor: InteractorSpeedRunSearch())
    setupRx(viewModel: viewModelList!)
    viewModelList?.fetch()


    override func didReceiveMemoryWarning()
    super.didReceiveMemoryWarning()


    override func viewWillAppear(_ animated: Bool)
    super.viewWillAppear(animated)
    navigationController?.isNavigationBarHidden = true



    private func setupCollectionView()
    registerCollectionCells()
    if #available(iOS 10.0, *)
    collectionView.isPrefetchingEnabled = false
    else
    // Fallback on earlier versions

    calculateLayoutCollectionItem()



    private func registerCollectionCells()
    collectionView.register(UINib(nibName: SpeedRunRowCollectionViewCell.nibName, bundle: nil),
    forCellWithReuseIdentifier: SpeedRunRowCollectionViewCell.reuseCellId)

    collectionView.register(UINib(nibName: SpeedRunCollectionViewCell.nibName, bundle: nil),
    forCellWithReuseIdentifier: SpeedRunCollectionViewCell.reuseCellId)



    private func calculateLayoutCollectionItem()
    if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
    layout.estimatedItemSize = CGSize.init(width: 2, height: 2)



    private func setupRx(viewModel: SpeedRunListViewModel)
    viewModel.numElements.asObservable().subscribe(onNext: e in
    self.collectionView.reloadData()
    , onError: error in

    , onCompleted:

    , onDisposed:

    ).disposed(by: disposeBag)


    buttonChangeLayout.rx.tap.subscribe(onNext: void in
    guard let value = self.viewModelList?.layoutRow else
    return

    self.viewModelList?.layoutRow = !value
    self.collectionView.collectionViewLayout.invalidateLayout()
    self.collectionView.reloadData()
    , onError: error in

    , onCompleted:

    , onDisposed:

    ).disposed(by: disposeBag)



    fileprivate func getCellId() -> String
    if let layoutRow = self.viewModelList?.layoutRow, layoutRow == true
    return SpeedRunRowCollectionViewCell.reuseCellId

    return SpeedRunCollectionViewCell.reuseCellId




    //MARK: - UICollectionViewDelegate, UICollectionViewDataSource
    extension SpeedRunListViewController: UICollectionViewDelegate,
    UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    guard let numElements = viewModelList?.numElements else
    return 0

    return numElements.value


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: getCellId(), for: indexPath) as! SpeedRunCollectionViewCellBase
    if let cellViewModel = viewModelList?.getCellViewModel(index: indexPath.row)
    cell.setupCell(viewModel: cellViewModel)


    return cell



    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    guard let speedRun = viewModelList?.getSpeedRun(index: indexPath.row) else
    return


    let interactorDetail: InteractorSpeedRunDetail = InteractorSpeedRunDetail(speedRun: speedRun)
    let viewControllerDetail: SpeedRunDetailViewController = SpeedRunDetailViewController(interactor: interactorDetail)
    viewControllerDetail.URISpeedRunDetail = (speedRun.links![1].uri)!

    navigationController?.pushViewController(viewControllerDetail, animated: true)






    And the truth is that I do not know why that conflict of layouts occurs. But it's driving me crazy ... I can not understand how the cells are not shown initially (because data is being received). What could it be?



    Thank you very much, any question you attach it to me.



    [CODE UPDATED]



    These is the code solution:



    //MARK: - UICollectionViewDelegateFlowLayout
    extension SpeedRunListViewController: UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAt indexPath: IndexPath) -> CGSize


    if let value = self.viewModelList?.layoutRow
    if value
    return CGSize(width: 320, height: 144)

    else
    return CGSize(width: 96, height: 162)



    return CGSize(width: 320, height: 144)












    share|improve this question


























      1












      1








      1








      I am creating an app with Swift 4, where I make a request to the API and I want to return a result on a CollectionView.



      But I get the following error, which I think is from constraints:



      enter image description here



      This block is repeated 100 times.



      And the result is that he does not paint any cells. Showing an image like this:



      enter image description here



      Unless I press the top button "CHANGE AUTOLAYOUT" twice. Which is when you paint the cells of the two display modes you have, and it looks like this:



      enter image description here



      And this:



      enter image description here



      But the problem is, initially nothing is shown and should be shown. And the error that I show you in the beginning appears.



      To help you a little, because I would say that the problem derives from the constrainst applied, I attach some images with the different constrainsts applied.



      The initial xib, where the collectionView is, are:



      enter image description here



      The cell that is initially loaded is:



      enter image description here



      The cell once we have changed the layout is this:



      enter image description here



      I attached the code of the main class, the ViewVontroller that controls the CollectionView:



      import UIKit
      import RxSwift
      import RxCocoa

      final class SpeedRunListViewController: UIViewController

      @IBOutlet private var collectionView: UICollectionView!
      @IBOutlet private var buttonChangeLayout: UIButton!

      private let disposeBag = DisposeBag()
      private var viewModelList: SpeedRunListViewModel?
      private var numElementsByCol: CGFloat = 3

      override func viewDidLoad()
      super.viewDidLoad()
      navigationController?.isNavigationBarHidden = true
      setupCollectionView()
      viewModelList = SpeedRunListViewModel(interactor: InteractorSpeedRunSearch())
      setupRx(viewModel: viewModelList!)
      viewModelList?.fetch()


      override func didReceiveMemoryWarning()
      super.didReceiveMemoryWarning()


      override func viewWillAppear(_ animated: Bool)
      super.viewWillAppear(animated)
      navigationController?.isNavigationBarHidden = true



      private func setupCollectionView()
      registerCollectionCells()
      if #available(iOS 10.0, *)
      collectionView.isPrefetchingEnabled = false
      else
      // Fallback on earlier versions

      calculateLayoutCollectionItem()



      private func registerCollectionCells()
      collectionView.register(UINib(nibName: SpeedRunRowCollectionViewCell.nibName, bundle: nil),
      forCellWithReuseIdentifier: SpeedRunRowCollectionViewCell.reuseCellId)

      collectionView.register(UINib(nibName: SpeedRunCollectionViewCell.nibName, bundle: nil),
      forCellWithReuseIdentifier: SpeedRunCollectionViewCell.reuseCellId)



      private func calculateLayoutCollectionItem()
      if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
      layout.estimatedItemSize = CGSize.init(width: 2, height: 2)



      private func setupRx(viewModel: SpeedRunListViewModel)
      viewModel.numElements.asObservable().subscribe(onNext: e in
      self.collectionView.reloadData()
      , onError: error in

      , onCompleted:

      , onDisposed:

      ).disposed(by: disposeBag)


      buttonChangeLayout.rx.tap.subscribe(onNext: void in
      guard let value = self.viewModelList?.layoutRow else
      return

      self.viewModelList?.layoutRow = !value
      self.collectionView.collectionViewLayout.invalidateLayout()
      self.collectionView.reloadData()
      , onError: error in

      , onCompleted:

      , onDisposed:

      ).disposed(by: disposeBag)



      fileprivate func getCellId() -> String
      if let layoutRow = self.viewModelList?.layoutRow, layoutRow == true
      return SpeedRunRowCollectionViewCell.reuseCellId

      return SpeedRunCollectionViewCell.reuseCellId




      //MARK: - UICollectionViewDelegate, UICollectionViewDataSource
      extension SpeedRunListViewController: UICollectionViewDelegate,
      UICollectionViewDataSource
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
      guard let numElements = viewModelList?.numElements else
      return 0

      return numElements.value


      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: getCellId(), for: indexPath) as! SpeedRunCollectionViewCellBase
      if let cellViewModel = viewModelList?.getCellViewModel(index: indexPath.row)
      cell.setupCell(viewModel: cellViewModel)


      return cell



      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
      guard let speedRun = viewModelList?.getSpeedRun(index: indexPath.row) else
      return


      let interactorDetail: InteractorSpeedRunDetail = InteractorSpeedRunDetail(speedRun: speedRun)
      let viewControllerDetail: SpeedRunDetailViewController = SpeedRunDetailViewController(interactor: interactorDetail)
      viewControllerDetail.URISpeedRunDetail = (speedRun.links![1].uri)!

      navigationController?.pushViewController(viewControllerDetail, animated: true)






      And the truth is that I do not know why that conflict of layouts occurs. But it's driving me crazy ... I can not understand how the cells are not shown initially (because data is being received). What could it be?



      Thank you very much, any question you attach it to me.



      [CODE UPDATED]



      These is the code solution:



      //MARK: - UICollectionViewDelegateFlowLayout
      extension SpeedRunListViewController: UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView,
      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAt indexPath: IndexPath) -> CGSize


      if let value = self.viewModelList?.layoutRow
      if value
      return CGSize(width: 320, height: 144)

      else
      return CGSize(width: 96, height: 162)



      return CGSize(width: 320, height: 144)












      share|improve this question
















      I am creating an app with Swift 4, where I make a request to the API and I want to return a result on a CollectionView.



      But I get the following error, which I think is from constraints:



      enter image description here



      This block is repeated 100 times.



      And the result is that he does not paint any cells. Showing an image like this:



      enter image description here



      Unless I press the top button "CHANGE AUTOLAYOUT" twice. Which is when you paint the cells of the two display modes you have, and it looks like this:



      enter image description here



      And this:



      enter image description here



      But the problem is, initially nothing is shown and should be shown. And the error that I show you in the beginning appears.



      To help you a little, because I would say that the problem derives from the constrainst applied, I attach some images with the different constrainsts applied.



      The initial xib, where the collectionView is, are:



      enter image description here



      The cell that is initially loaded is:



      enter image description here



      The cell once we have changed the layout is this:



      enter image description here



      I attached the code of the main class, the ViewVontroller that controls the CollectionView:



      import UIKit
      import RxSwift
      import RxCocoa

      final class SpeedRunListViewController: UIViewController

      @IBOutlet private var collectionView: UICollectionView!
      @IBOutlet private var buttonChangeLayout: UIButton!

      private let disposeBag = DisposeBag()
      private var viewModelList: SpeedRunListViewModel?
      private var numElementsByCol: CGFloat = 3

      override func viewDidLoad()
      super.viewDidLoad()
      navigationController?.isNavigationBarHidden = true
      setupCollectionView()
      viewModelList = SpeedRunListViewModel(interactor: InteractorSpeedRunSearch())
      setupRx(viewModel: viewModelList!)
      viewModelList?.fetch()


      override func didReceiveMemoryWarning()
      super.didReceiveMemoryWarning()


      override func viewWillAppear(_ animated: Bool)
      super.viewWillAppear(animated)
      navigationController?.isNavigationBarHidden = true



      private func setupCollectionView()
      registerCollectionCells()
      if #available(iOS 10.0, *)
      collectionView.isPrefetchingEnabled = false
      else
      // Fallback on earlier versions

      calculateLayoutCollectionItem()



      private func registerCollectionCells()
      collectionView.register(UINib(nibName: SpeedRunRowCollectionViewCell.nibName, bundle: nil),
      forCellWithReuseIdentifier: SpeedRunRowCollectionViewCell.reuseCellId)

      collectionView.register(UINib(nibName: SpeedRunCollectionViewCell.nibName, bundle: nil),
      forCellWithReuseIdentifier: SpeedRunCollectionViewCell.reuseCellId)



      private func calculateLayoutCollectionItem()
      if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
      layout.estimatedItemSize = CGSize.init(width: 2, height: 2)



      private func setupRx(viewModel: SpeedRunListViewModel)
      viewModel.numElements.asObservable().subscribe(onNext: e in
      self.collectionView.reloadData()
      , onError: error in

      , onCompleted:

      , onDisposed:

      ).disposed(by: disposeBag)


      buttonChangeLayout.rx.tap.subscribe(onNext: void in
      guard let value = self.viewModelList?.layoutRow else
      return

      self.viewModelList?.layoutRow = !value
      self.collectionView.collectionViewLayout.invalidateLayout()
      self.collectionView.reloadData()
      , onError: error in

      , onCompleted:

      , onDisposed:

      ).disposed(by: disposeBag)



      fileprivate func getCellId() -> String
      if let layoutRow = self.viewModelList?.layoutRow, layoutRow == true
      return SpeedRunRowCollectionViewCell.reuseCellId

      return SpeedRunCollectionViewCell.reuseCellId




      //MARK: - UICollectionViewDelegate, UICollectionViewDataSource
      extension SpeedRunListViewController: UICollectionViewDelegate,
      UICollectionViewDataSource
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
      guard let numElements = viewModelList?.numElements else
      return 0

      return numElements.value


      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: getCellId(), for: indexPath) as! SpeedRunCollectionViewCellBase
      if let cellViewModel = viewModelList?.getCellViewModel(index: indexPath.row)
      cell.setupCell(viewModel: cellViewModel)


      return cell



      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
      guard let speedRun = viewModelList?.getSpeedRun(index: indexPath.row) else
      return


      let interactorDetail: InteractorSpeedRunDetail = InteractorSpeedRunDetail(speedRun: speedRun)
      let viewControllerDetail: SpeedRunDetailViewController = SpeedRunDetailViewController(interactor: interactorDetail)
      viewControllerDetail.URISpeedRunDetail = (speedRun.links![1].uri)!

      navigationController?.pushViewController(viewControllerDetail, animated: true)






      And the truth is that I do not know why that conflict of layouts occurs. But it's driving me crazy ... I can not understand how the cells are not shown initially (because data is being received). What could it be?



      Thank you very much, any question you attach it to me.



      [CODE UPDATED]



      These is the code solution:



      //MARK: - UICollectionViewDelegateFlowLayout
      extension SpeedRunListViewController: UICollectionViewDelegateFlowLayout

      func collectionView(_ collectionView: UICollectionView,
      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAt indexPath: IndexPath) -> CGSize


      if let value = self.viewModelList?.layoutRow
      if value
      return CGSize(width: 320, height: 144)

      else
      return CGSize(width: 96, height: 162)



      return CGSize(width: 320, height: 144)









      ios swift uicollectionview constraints uicollectionviewflowlayout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 10:11







      Jordi Gallen

















      asked Nov 14 '18 at 13:22









      Jordi GallenJordi Gallen

      275




      275






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You are not setting the UICollectionViewDelegateFlowLayout in the viewController. You need to set it and then use



          func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout,
          sizeForItemAt indexPath: IndexPath) -> CGSize


          To set the sizes of your cells.



          You are having the error because when you load the cells the very first time you are basically telling them that they have a size of 0 0.






          share|improve this answer























          • Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

            – Jordi Gallen
            Nov 15 '18 at 10:17










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301245%2fswift4-i-can-not-show-the-collectionview-the-first-time-unable-to-simultaneou%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You are not setting the UICollectionViewDelegateFlowLayout in the viewController. You need to set it and then use



          func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout,
          sizeForItemAt indexPath: IndexPath) -> CGSize


          To set the sizes of your cells.



          You are having the error because when you load the cells the very first time you are basically telling them that they have a size of 0 0.






          share|improve this answer























          • Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

            – Jordi Gallen
            Nov 15 '18 at 10:17















          1














          You are not setting the UICollectionViewDelegateFlowLayout in the viewController. You need to set it and then use



          func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout,
          sizeForItemAt indexPath: IndexPath) -> CGSize


          To set the sizes of your cells.



          You are having the error because when you load the cells the very first time you are basically telling them that they have a size of 0 0.






          share|improve this answer























          • Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

            – Jordi Gallen
            Nov 15 '18 at 10:17













          1












          1








          1







          You are not setting the UICollectionViewDelegateFlowLayout in the viewController. You need to set it and then use



          func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout,
          sizeForItemAt indexPath: IndexPath) -> CGSize


          To set the sizes of your cells.



          You are having the error because when you load the cells the very first time you are basically telling them that they have a size of 0 0.






          share|improve this answer













          You are not setting the UICollectionViewDelegateFlowLayout in the viewController. You need to set it and then use



          func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout,
          sizeForItemAt indexPath: IndexPath) -> CGSize


          To set the sizes of your cells.



          You are having the error because when you load the cells the very first time you are basically telling them that they have a size of 0 0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 0:05









          Galo Torres SevillaGalo Torres Sevilla

          1,0052311




          1,0052311












          • Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

            – Jordi Gallen
            Nov 15 '18 at 10:17

















          • Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

            – Jordi Gallen
            Nov 15 '18 at 10:17
















          Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

          – Jordi Gallen
          Nov 15 '18 at 10:17





          Perfect! Thank you @GaloTorresSevilla This solve my problem :) ! I have updated my code.

          – Jordi Gallen
          Nov 15 '18 at 10:17



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301245%2fswift4-i-can-not-show-the-collectionview-the-first-time-unable-to-simultaneou%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo