Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Dynamic height for UICollectionView inside StackView

I am trying to figure out how to set AutoLayout constraints for a UICollectionView inside of a UIStackView (or without the StackView) and feel confused. The other answers on StackOverflow that I have found do not seem to directly apply.

Here is what I have:

-----------------------------------
|  -----------------------------  |
|  |    UICollectionView # 1    | | (Sticky header for whole view)
|  -----------------------------  |
|  -----------------------------  |
|  |    UICollectionView # 2    | | (Table/grid with sections)
|  -----------------------------  |
|  -----------------------------  |
|  |    Button                  | |
|  -----------------------------  |
|  -----------------------------  |
|  |    UICollectionView # 3    | | (Sticky footer)
|  -----------------------------  |

I am aware that there are now stick section headers for collection views, but collection #2 has it's own sections, so these are sticky headers for the whole view, which doesn't exist.

I can set constraints for views #1, #3, and the button, but #2's height is unknown. How do I set it so that view can dynamically change height, BUT view #3 is always visible.

I.e. View #2 will start to scroll if it becomes too long, and view #3 will stay visible.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you want a self-sizing collection view, you have to create an NSLayoutConstraint for the collection view's height:

@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!

Then, in viewDidLayoutSubviews, update the height constraint and layout the view:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
    view.layoutIfNeeded()
}

When you reload your collection view, make sure to call viewDidLayoutSubviews:

collectionView.reloadData()
viewDidLayoutSubviews()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda